2.3 Variables

Within a VBA Environment, a variable is a mutable container of data values (section 2.1). While individual data values are immutable and do not change while a program executes, the data value contained by a particular variable can be replaced many times during the program’s execution.

Specific variables are defined either by the text of a VBA program, by the host application, or by this specification. The definition of a variable includes the specification of the variable’s declared type (section 2.2).

Variables have a well-defined lifecycle, they are created, become available for use by the program, and are then subsequently destroyed. The span from the time a variable is created to the time it is destroyed is called the extent of the variable. Variables that share a creation time and a destruction time are can be said to share a common extent. The extent of a variable depends upon how it was defined but the possible extents are defined by the following table.

Extent Name

Variable Definition Form

Variable Lifespan

Program Extent

Defined by the VBA specification or by the host application.

The entire existence of an active VBA Environment.

Module Extent

A Module Variable Declaration or a

static local variable declaration within a procedure.

The span from the point that the containing module is incorporated into an active VBA project to the point when the module or project is explicitly or implicitly removed from its VBA Environment.

Procedure Extent

A procedure local variable or formal parameter declaration of a procedure.

The duration of a particular procedure invocation.

Object Extent

A variable declaration within a class module.

The lifespan of the containing object.

Aggregate Extent

A dependent variable (section 2.3.1) of an array or UDT variable.

The lifespan of the variable holding the containing aggregate data value (section 2.1.1).

 

When a variable is created, it is initialized to a default value. The default value of a variable is determined by the declared type of the variable according to the following table.

Declared Type

Initial Data Value

Boolean

False

Byte, Currency, Double, Integer, Long, LongLong

0 value of the corresponding value type (section 2.1)

Double or Single

+0.0 value of the corresponding value type

Date

30 December 1899 00:00:00

String

The empty string

Variant

Empty

String*n, where n is an integer between 1 and 65,526

A string of length n consisting entirely of the implementation dependent representation of the null character corresponding to Unicode codepoint U+0000.

Fixed size array whose declared element type is one of Boolean, Byte, Currency, Data, Double, Object, Single,

String, or String*n

The array data value whose number of dimensions and bounds are identical with the array’s declared dimensions and bounds and whose every element is the default data value of the declared element type.

Fixed size array whose declared element type is Variant

The array value whose number of dimensions and bounds are identical with the array’s declared dimensions and bounds and whose every element is the value Empty.

Resizable array whose declared element type is one of Boolean, Byte, Currency, Data, Double, Object, Single,

String, or String*n

An array value with no dimensions.

Resizable array whose declared element type is Variant

An array value with no dimensions.

Object or a Specific class name

The value Nothing.

Specific UDT name

The UDT data value for the named UDT type whose every named

element has the default data value from this table that is appropriate for that element’s declared type.

 

Variables generally have a single variable name that is used to identify the variable within a VBA program. However, variable names have no computational significance. Some situations such as the use of a variable as a reference parameter to a procedure invocation can result in multiple names being associated with a single variable. Access to variables from within a VBA program element is determined by the visibility scopes of variable names. Typically, a variable name’s visibility is closely associated with the variable’s extent but variable name scopes themselves have no computational significances.