Microsoft Office Tutorials and References
In Depth Information
This data type’s flexibility comes from its ability to hold almost any type of data (with the single
exception of fixed-length strings). Thus, a type Variant can hold numbers, text, object
references, user-defined types, and arrays. One common use for this data type is when data needs to
be treated either as text or as a number depending on circumstances. Another use is as an
argument to procedures that can take different kinds of data. You should not, however, simply use type
Variant as a convenience to avoid the necessity of thinking about what data type should be used
for specific variables. The Variant data type requires more memory to store, and more processor
time to manipulate, than other data types.
Object Variables
Another type of variable holds a reference to an object. Objects , which represent the various parts
of the Outlook program (among other things), are a central part of VBA programming. You learn
more about object variables when you look at Outlook’s Object Model in Chapter 24.
An array is a group of two or more variables that have the same name and are distinguished by a
numeric index. VBA offers two types of arrays: static and dynamic.
Static Arrays
A static array is created with a specified number of elements and this number does not change
while the program is executing. To declare a static array, use the following syntax:
Dim ArrayName(n) As type
ArrayName is the name of the array, which follows the same naming rule as regular VBA
variables. n is the number of elements in the array, and type can be any of VBA’s data types. Here’s an
Dim NewArray(100) As Long
This statement creates an array of type Long variables. VBA starts array indexes at 0; this array
actually contains 101 elements, at indexes 0 through 100. You access an array’s elements using any
expression to specify the index. For example:
NewArray(1) = 25
x = 15
NewArray(x) = 99 ‘ Same as NewArray(15)
NewArray(MyArray(1)) = 5 ‘ Same as NewArray(25)
You can also create arrays where the index does not start at 0 by using the To keyword, which lets
you specify any starting index you like. The format is as follows:
Dim NewArray( start To stop )
Search JabSto ::

Custom Search