
Creating code snippets with the Snippet editor
A common requirment for programmers is to collect pieces of code that occur regularly in programs for reuse.
Code VBA includes a code library which can be extended by you with complete procedures or a couple of lines of code, the latter are called code snippets.
Here it is explained how to make a code snippet, the extra syntax allowed on top of the regular VBA and how to manage them.
- Adding .vba snippets
- .vba snippet syntax
- Managing code snippets
- Customize the procedure header and change comment snippet
Adding .vba snippets
When you intend to create a code snippet of your own, the first question is where will be the best place to store it. What location in the menu will be most likely to look for it. Here are some suggestions:
- If a snippet is generally used in all Office applications, then put it in the most appropriate subfolder of the VBA menu
- If it is specific for a certain class in an Office application, add it to that class's subfolder. In the above, snippets appearing under ActiveCell are stored in the Range subfolder of Excel.
- If it concerns a generally useful function like IsValidSheetname, store it under the Application class subfolder of Excel. It will then be available from the (Excel) Application menu
After you decided what is the best location to put it, following the menu to the selected point and press Open Libary
.vba snippet syntax
A snippet is essentially just some VBA code, optionally with optionally some special tags.
The example snippet below illustrates things you can do with snippets. Press < to open a selection list of available tags. Select an item and press Enter.
<cursor>; lets you determine where the cursor should appear after the snippet is inserted. If not specified the cursor will be at the end of the snippet
<Date> Tells the snippet insertion to try to find a variable of the selected type (here Date) or insert a new variable. If more than one variable of this type is found only a prefix is inserted, allowing the user to use intellisense to select the appropriate variable
<String:File> Adding a name after : inside the tag allows you to propose a filename in case a new variable is to be created - in this example the variable name will be strFile.
<Range> Alternatively, you can construct a tag with a typename yourself.
<include> An advanced use allows you to specify a module, form or class to be inserted when using the snippet. In the case below the module contained the Windows API call for getting the Temp folder.
<select.Action:.Display,.Send,.Save> let the user select for field Action one of three options. Can only be used in snippets with multiple variables.

Managing code snippets
In addition to creating snippets you will also want to edit, copy or remove them. You will be able to do all these using Windows Explorer. Using the Open library on the menu where the snippet you want to to work on opens Explorer in the correct folder location.
Edit an existing snippet
To open an existing snippet for editing
- In the menu go to where the snippet is located
- Select Open library - use shortcut O
Windows Explorer opens in the folder containing the snippet - Open the snippet using double-click or right-click » Open

Customize the procedure header and change comment snippet
Two snippets you are very likely to want to customize are the 'Procedure Header' and 'Code Change' snippets. These can be found under the Comment menu.