Microsoft Office Tutorials and References
In Depth Information
Using Arrays
Dim MyArray(1 To 100) As Long
When you declare an array, you need to specify only the upper index, in which case VBA (by
default) assumes that 0 is the lower index. Therefore, the following two statements have the
same effect:
Dim MyArray(0 to 100) As Long
Dim MyArray(100) As Long
In both cases, the array consists of 101 elements.
If you want VBA to assume that 1 is the lower index for all arrays that declare only the upper
index, include the following statement before any procedures in your module:
Option Base 1
If this statement is present, the following two statements have the same effect (both declare an
array with 100 elements):
Dim MyArray(1 to 100) As Long
Dim MyArray(100) As Long
Declaring multidimensional arrays
The array examples in the preceding section are one-dimensional arrays. VBA arrays can have up
to 60 dimensions, although it’s rare to need more than 3 dimensions (a 3-D array). The following
statement declares a 100-integer array with two dimensions (2-D):
Dim MyArray(1 To 10, 1 To 10) As Long
You can think of the preceding array as occupying a 10 x 10 matrix. To refer to a specific element
in a 2-D array, you need to specify two index numbers. For example, here’s how you can assign a
value to an element in the preceding array:
MyArray(3, 4) = 125
A dynamic array does not have a preset number of elements. You declare a dynamic array with a
blank set of parentheses:
Dim MyArray() As Long
Search JabSto ::

Custom Search