Declaring arrays

You declare an array with a Dim or Public statement, just as you declare a regular variable. You can also specify the number of elements in the array. You do so by specifying the first index number, the keyword To, and the last index number — all inside parentheses. For example, here's how to declare an array comprising exactly 100 integers:

Dim MyArray(1 To 100) As Integer

When you declare an array, you need specify only the upper index, in which case VBA assumes that 0 is the lower index. Therefore, the two statements that follow have the same effect:

Dim MyArray(0 to 100) As Integer Dim MyArray(100) As Integer

In both these cases, the array consists of 101 elements.

If you would like 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

0 0

Post a comment