Creating and using code fragments

Code fragments (synonym: snippet) are pieces of code that occur regularly in programs and for that reason have been collected for reuse. Code VBA includes a code fragment library which can be extended by you.

fragments appear at the top

Inserting a fragment

Code fragments always appear at the top of the sub menu. Code fragments don't have shortcut keys and for this reason must be selected using down-arrow and Enter or using the mouse. To see what a fragment contains either move to it using the cursor and press F1 or hover the cursor above it.

Tooltip showing fragment code

Click this Start screencast showing fragments being inserted to see how to add fragmentsInsert fragment screencast

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

Fragments are files stored in a \My Documents\VBA Code\ subfolder - as an example the fragments 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 fragment files.

Adding standard modules

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

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

Adding .vba fragments

When you intend to create a code fragment 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. As an example, we take the Switch fragment which is not yet included. The switch statement is a compact way to relate an output value to a given input thus specifying a relationship using code. In this example the function matchLanguagecan tell us what language is spoken in Rome. Because it resembles the Select Case statement, I decide to put it there.

Function matchLanguage(ByVal cityName As StringAs String
    matchLanguage = CStr(Switch( _
        cityName = "London", "English", _
        cityName = "Rome", "Italian", _
        cityName = "Paris", "French"))
End Function

.vba Fragment syntax

A fragment is essentially just some VBA code, optionally with some fragment tags.

The example fragment below illustrates things you can do with fragments. Press < to open a selection list of available tags. Select an item and press Enter.

fragment syntax

<cursor>; lets you determine where the cursor should appear after the fragment is inserted. If not specified the cursor will be at the end of the fragment

<Date> Tells the fragment 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 to be inserted when using the fragment. In the case below the module contained the Windows API call for getting the Temp folder.

Managing code fragments

In addition to creating fragments you will also want to edit, copy or remove them. You will be able to do all these using Windows Explorer. Using the Manage Fragments on the menu where the fragment you want to to work on opens Explorer in the correct folder location.

Edit an existing fragment

To open an existing fragment for editing

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

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

comment menu

Customize the procedure header and change comment fragment

Two fragments you are very likely to want to customize are the 'Procedure Header' and 'Code Change' fragments. These can be found under the Comment menu.