snippets appear as part of main menu

Creating and using code snippets

Code snippets (synonym: snippets) are pieces of code that occur regularly in programs and for that reason have been collected for reuse. Code VBA includes a code snippet library which can be extended by you with complete procedures or a couple of lines of code.

Inserting a snippet

Code snippets generally appear in the Library sub menu of an object. In some special cases they appear at directly in the main menu - the image on the right shows such a case for VBA »Manage File. Code snippets can be inserted using the first character or using down-arrow and Enter or using the mouse. Depending on the number of variable involved in the snippet, the code is inserted directly (0 variables), a submenu may appear letting you select a candidate value or variable (1 variable), or a snippet inserter dialog opens allowing you to specify multiple values. To see what a snippet contains either move to it using the cursor and press F1 or hover the cursor above it.

Tooltip showing snippet code

Click this Start screencast showing snippets being inserted to see how to add snippetsInsert snippet screencast

You may also want to extend the library with your own snippets. This is discussed in the following sections.

Snippets are files stored in a \My Documents\VBA Code\ subfolder - as an example the snippets on the right image are located in \My Documents\VBA Code\VBA\Manage File\. They can be normal procedures in standard code modules (.bas, .frm, .cls) or snippets stored in .vba snippet files.

Adding standard modules

To add a module containing procedures to a suitable location in the VBA code library:

  1. Determine the location where adding it is most appropriate - below
  2. Use the VBE built in Export feature menu: File » Export File (or CRTL-E).
  3. In the dialog that pops up browse to the folder you identified in step 1. and press Save
Tip
All public procedure in a module inserted in a snippets subfolder appear in the menu. By make certain procedures Private you can hide them.

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:

  1. If a snippet is generally used in all Office applications, then put it in the most appropriate subfolder of the VBA menu
  2. If it is specific for a certain class in an Office applications, add it to that class's subfolder. In the above, snippets appearing under ActiveCell are stored in the Range subfolder of Excel.
  3. 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 some snippet 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.

snippet syntax

<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.

a sofisticated snippet with options

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

  1. In the menu go to where the snippet is located
  2. Select Open library - use shortcut O
    Windows Explorer opens in the folder containing the snippet
  3. Open the snippet using double-click or right-click » Open

Click this How to open an existing snippet for editing screencast to see how to open an existing snippet for editing open snippet screencast

comment menu

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.