VBA naming conventions

When naming program elements such as procedures and variables in VBA it is best practice to use 'Hungarion notation' with CamelCase an prefixing with a type shorthand.

It is useful to prefix variables so as to indicate their type:

  • Using a variable with type prefix helps discern its use.
  • With intellisense you can easily select the variable

Common prefixes are: str (String), int (Integer), dt (Date), boo (Boolean), var (Variant), obj (Object), c for constant and arr for array.

To get intellisense popup, type the prefix and Ctl-[space]

Click this Start demo screencast to see how intellisense displays variables in your code that have the prefix you typed.Name file string selection demo

Many programmers use the Reddick's naming conventions ( http://www.xoc.net/standards/rvbanc.asp ) or a shorter version you developed yourself. Code VBA supports Reddick as a default, but lets you customize it to fit your needs.

Support of the naming conventions consists of automatically adding the appropriate prefix (optional). Considering the number of classes that may be used - 300+ in Excel -, not every type has a prefix defined. We implemented an algorithm that proposes a prefix in case it is not present in the prefixes file. If you don't like it, add a better one to the file.e.

To change or add prefixes open the file that contains the prefixes:

the prefixes are stored in a file called prefix.txt. Below you see a bit of it's content.


Class=cls
Collection=col
Module=m
Global=g
Const=c
ByRef=r
ByVal=v
Array=arr
Constant=c
AccessObject=aob
AccessObjectProperties=aops
AccessObjectProperty=aop
Animation=ani
Application=app
Band=bnd
Bands=bnds