VBA Program Identifiers: Data Type, Scope, and Lifetime

VBA programs often feature named discrete entities called identifiers (e.g., variables). Here, we get familiar with these vital program elements, their attributes, and the rules for legally naming them in VBA.

In this article:

5 minutes read

Program identifiers (or identifiers) are discrete and manipulable elements of a VBA program. They refer to the distinct named entities used in the code lines or blocks that make up a VBA program. The table below summarizes the most common VBA program identifiers and their attributes.

IdentifierDescriptionData TypeScopeLifetime
VariableA named memory location that stores data (of a certain type) whose value can be altered during program execution (i.e., at runtime).
ConstantA symbolic and meaningful name assigned a value, at compile time (i.e., before running a program), that cannot be altered at runtime.
Enumeration (or Enum)A predefined set of related constants, each assigned an integer value.
ProcedureA code block beginning with a Sub, Function, Property, or Event statement and finishing with a End Sub, End Function, or End Property) statement.
Procedure ArgumentData (of specific type and value) fed into procedures to facilitate their execution.
Function ReturnData (of specific type and value) returned by a function upon execution.
ObjectsInstances (i.e., clones) of a class. A class is a code-template for creating objects, it is a structure comprised of both data and the procedures for manipulating it.
PropertyA class/object’s data denoting its attributes or characteristics.
MethodA class/object’s procedures designed to manipulate its data (i.e., properties).
EventProcedures triggered by internal (e.g. running out of memory) and external (e.g. mouse clicks) actions or occurrences detected by a program.

Applicable

Inapplicable

Data Type of Identifiers

Data Type refers to the nature of the values that an identifier can store. VBA supports both built-in and user-defined data types. The table below summarizes the intrinsic VBA data types.

Data TypeMemory Space (32-bit)Value Range
Boolean2 bytesTrue or False
Byte1 byte0 to 255 (integers)
Integer2 bytes-32,768 to 32,767 (integers)
Long4 bytes-2,147,483,648 to 2,147,483,647 (integers)
Single4 bytes-3.402E38 to -1.401E-45 (negative decimals)
1.401E-45 to 3.402E38 (positive decimals)
Double8 bytes-1.797E308 to -4.940E-324 (negative decimals)
4.940E-324 to 1.797E308 (positive decimals)
String (variable-length)10 bytes + String length0 to 2,147,483,648 characters
String (fixed-length)String length1 to 65,536 characters
Variant (numeric)16 bytesNumbers, same range as Double
Variant (textual)22 bytes + String lengthText, same range as a variable-length String
Object4 bytesObject reference (i.e., addresses in memory where data of any type is stored)

Initial Value of Identifiers

Initial value refers to the default value stored in an identifier upon declaring it. As a result of declaring an identifier, the compiler reserves memory space for its values. The size of the space allocated corresponds to the identifier’s data type.

The compiler also stores the identifier’s default value in the allotted memory space. The table below shows the initial values of common VBA intrinsic data types. If a new value is specified during a declaration, it replaces the initial value in memory.

Data TypeInitial Value
BooleanFalse
Byte, Integer, Long, Single, and DoubleEmpty number, i.e. zero (0)
String (variable-length)Empty string, i.e. a zero-length string (“”)
String (fixed-length)String comprised of the same number of ASCII character code 0, or Chr( 0 ), as the string length
Variant (numeric)Empty number, i.e. zero (0)
Variant (textual)Empty string, i.e. a zero-length string (“”)
ObjectNothing, i.e., a null reference or reference to nowhere in memory

Identifier Scope Levels

Scope refers to the visibility of an identifier to code inside or outside a VBA project (i.e., program). An identifier’s scope is the collection of all code that can reference it without qualifying its name. Qualifying means using the dot operator (.) to trace an identifier’s location inside or outside a project.

An identifier may be visible to code within the same block, procedure, module, or project as itself. Moreover, it may also be visible to code outside its own project. The graphic below describes the different VBA identifier scope levels.

VBA Program Identifiers – Scope Levels [from the narrowest (block level) to broadest (application level)]
VBA Program Identifier Scope Levels – from the narrowest (block level) to broadest (application level)

Concerning the graphic above, the following additional details are noteworthy:

  • You must declare module-level identifiers outside all procedures in a module. Moreover, it is preferable to have their declaration at the top of their module. Another name for this scope is the Private Module level, as its identifiers are visible only to code in their declaration module. Also, their declarations include the Private keyword.
  • You must declare project-level identifiers outside all procedures, in all modules, in a project. Moreover, it is preferable to have their declaration at the top of their module. Another name for this scope is the Public Module level, as its identifiers are visible to code in all a project’s modules. Furthermore, their declarations include the Public keyword.
  • You must declare application-level identifiers outside all procedures, in all modules, in a project. Moreover, it is preferable to have their declaration at the top of the module. Also, their declaration modules can only either be in Standard or Class modules, with the Public keyword. Moreover, code in external projects can see such identifiers only after setting references to their declaration project.

Lifetime of Identifiers

Lifetime refers to the period during which an identifier stores a value. The stored value may change as the program runs, but the identifier always holds value during its lifetime.

Furthermore, lifetime only applies to variables. But, as shown in this article’s first table, few other identifiers are treated as variables in the context of lifetime. These include procedures, procedure arguments, and function returns.

Lifetime ties into scope since an identifier no longer holds a value once it runs out of scope. But, you can extend a procedure-level identifier’s lifetime by using the Static keyword while declaring it. Another name for such a declaration statement is a Static statement.

Static Statement’s LocationStatic Variable’s Lifetime StartsStatic Variable’s Lifetime Ends
In a Standard moduleAll procedure-level variables are initialized once the procedure starts running (i.e., memory space is allocated, and initial values stored there). The same applies to block-level variables, regardless of whether the block ever runs.

For these variables, lifetime begins the first time the procedure in which they are declared runs.
Values are retained until the module is reset or restarted (i.e., application stops running).
In a Class moduleSame as aboveValues are retained in each instance of the class (i.e., object) until that instance is destroyed (i.e., no longer referenced by any variable).
In a Form moduleSame as aboveValues are retained until the Form is closed (i.e., Form’s UnLoad method is run, or the application stops running).

Moreover, using the Static keyword in the declaration of a procedure (or method) makes all procedure-level variables in the procedure static. Consequently, all procedure-level variables in such Static procedures retain their values from the first time the procedure runs until the program ends.

Rules for Naming VBA Program Identifiers

Having discussed what identifiers are and their attributes, let’s now take a look at how to legally (so to speak) name them. Typically, your code will contain numerous instances of the same type of identifier, e.g. several variables. So, you’ll need to give each instance a unique name. While doing so, you must adhere to the following set of VBA naming rules:

  • The maximum number of characters allowed in a name is 255;
  • Names must begin with an alphabet or letter;
  • Space, period or dot (.), exclamation mark (!), and symbols @, &, # are invalid;
  • Avoid using names that match any VBA keywords (i.e., reserved identifier) for program elements like procedures, statements, methods, and built-in constants;
  • It is illegal to use the same name for different identifiers that have the same scope level.

In addition to the above technical rules, you should also endeavour to use concise, meaningful, easily and quickly typed and read names.

Furthermore, VBA is case-insensitive, meaning that the compiler treats the names “anakin” and “ANAKIN” as the same. However, VBA preserves the case used in an identifier’s declaration statement. So, be consistent and stick to one case (or case combination) for all mentions of an identifier’s name throughout your program.

Lastly, there are several naming conventions adopted by programmers in their everyday practice. The most common of these are camelCase, PascalCase, and snake_case, and are the subject of a separate article.

5 1 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Stephen Gay
November 19, 2020 10:08 AM

I mainly read this article being interested in the life of objects. I reread the article a few times but it wasn’t clear, about their lifetime(to me)”Furthermore, lifetime only applies to variables”

Keep Learning

Related Articles

Keep Learning

Related Articles