Declaring arrays

Before you can use an array, you must declare it. You declare an array with a Dim or a Public statement, just as you declare a regular variable. However, you also need to specify the number of elements in the array. You do this by specifying the first index number, the keyword to, and the last index number — all inside parentheses. The following example shows how to declare an array of 100 integers:

Dim MyArray(1 to 100) As Integer

When you declare an array, you can choose to specify only the upper index. VBA assumes that 0 is the lower index. Therefore, the following statements both declare the same 101-element array:

Dim MyArray(0 to 100) As Integer

Dim MyArray(10 0) As Integer

If you want VBA to assume that 1 is the lower index for your arrays, simply [fOll include the following statement in the Declarations section of your module:

Option Base 1

This statement forces VBA to use 1 as the first index number for arrays that declare only the upper index. If this statement is present, the following statements are identical, both declaring a 100-element array:

Dim MyArray(1 to 100) As Integer

Dim MyArray(10 0) As Integer

0 0

Post a comment