Array Upper and Lower Bounds

By default VBA assigns zero (0) to the first element of the array. Therefore, number 1 represents the second element of the array, number 2 represents the third, and so on. With numeric indexing starting at 0, the one-dimensional array cities(6) contains seven elements numbered from 0 to 6. If you'd rather start counting your array's elements at 1, you can explicitly specify a lower bound of the array by using an Option Base 1 statement. This instruction must be placed in the declaration section at the top of a VBA module before any Sub statements. If you don't specify Option Base 1 in a procedure that uses arrays, VBA assumes that the statement Option Base 0 is to be used and begins indexing your array's elements at 0.

You can have the array indexing start at a number other than 0 or 1. To do this, you must specify the bounds of an array when declaring the array variable. The bounds of an array are its lowest and highest indices. Let's take a look at the following example:

Dim cities(3 To 6) As Integer

The above statement declares a one-dimensional array with four elements. The numbers enclosed in parentheses after the array name specify the lower (3) and upper (6) bounds of the array. The first element of this array has the number 3, the second—4, the third—5, and the fourth—6. Notice the keyword To between the lower and the upper index.

Tip 7-3: The Range of the Array

The spread of the subscripts specified by the Dim statement is called the range of the array (for example: Dim mktgCodes(5 To 15)).

0 0

Post a comment