Building Reusable Code Libraries

After you have been writing Microsoft® Visual Basic® for Applications (VBA) code for any length of time, you will find you must perform certain operations regularly. For example, you often might have to parse a file path and return just the name of the file, or you might have to write procedures to log errors to a text file. Rather than re-creating these procedures each time you must use them, you can store them in a code library and reuse them in other VBA projects.

There are two ways that you can create a reusable code library programmatically:

  • By creating a DLL in Visual Basic 4.0 or later
  • By creating an application-specific template or add-in

Whatever method you choose, you must always set a reference from the VBA project to the file containing the code library. Even if the file is a template or add-in, the procedures it contains will not be available to your VBA project unless you set a reference to it. When you do set a reference to a code library, you can call its procedures from your VBA project.

In This Section