Skip to main content

Variables

String

String variables are used to hold text data.  A fixed-length string can be declared to contain anywhere from 1 to about 64,000 characters.  To declare a fixed-length string, use the String keyword followed by the * (asterisk) symbol and the desired size.

Date

The term date is used to refer to both dates and times.  Dates are represented numerically as floating-point numbers. The integer part of the number represents the date as the number of days since December 30, 1899 (with negative numbers for prior dates), and the fractional part of the number represents the time of day as a fraction of the 24-hour day.

Object

The Object data type can hold a reference to any object.  Using object any type of object can be referenced while you can reference specific types of objects with they type of object, such as referencing a database as a database object or a task as a task object.

Boolean

A Boolean variable can hold a True/False value. Boolean variables (and properties) are used frequently in VBA programming to hold data that can be on/off, yes/no, and so on. When you declare a Boolean variable, it is automatically initialized to False.

Variant

Variant is VBA’s most flexible data type as well as the default type. The Variant data type can hold almost any type of data, the exceptions being fixedlength strings and user-defined types. The downside is that Variant data requires more memory to store, and more computer power to process, than other data types.

User-Defined Types

A user-defined type (UDT) allows the programmer to define custom data elements that are specifically suited for the data at hand. A UDT can contain two or more elements, each element being a variable or array. UDTs are defined with the Type...End Type statement.

Nothing

There are situations where an object variable does not refer to any object: It refers to nothing, and VBA has the special keyword Nothing to represent this. An object variable contains Nothing when it has been declared but not yet initialized (has not been assigned an object reference).

Finally, you can (and should) explicitly set an object reference to Nothing when you are finished using the object

Static Arrays

A static array has a fixed size or number of elements. You specify the array size when you declare it, and the size cannot change during program execution.  The size will be one more than n because VBA array indexes start by default at 0.

You can use an integer variable or constant as the index.

Dynamic Arrays

A dynamic array does not have a fixed size. It can be enlarged or shrunk as needed by code during program execution. A dynamic array is declared by using empty parentheses in the Dim statement.

Before using the array, you must set its size using the ReDim statement.