Declare an Array

Declare an array by adding parentheses after the array name. The parentheses contain the number of elements in the array: Dim myArray (2)

This creates an array, myArray, that contains three elements. Why three? Because, by default, the index count starts at zero:

If the index count needs to start on one, then use Option Base 1 . This forces the count to start at one. The option base statement is placed in the declarations section of the module:

Option Base 1

Dim myArray(2)

This now forces the array to have only two elements.

You can also create an array independent of the Option Base statement by declaring its lower bound:

Dim myArray (1 to 10) Dim BigArray (100 to 200)

Declare an Array 379

Fill an Array 380

Empty an Array 382

Arrays Can Make It Easier to Manipulate Data, But Is That All? 383

Dynamic Arrays 385

Passing an Array 386

Next Steps 386

Every array has a lower bound (Lbound) and an upper bound (Ubound). When you declare Dim myArray (2), you are declaring the upper bound and allowing the option base to declare the lower bound. By declaring Dim myArray (1 to 10), you declare the lower bound, 1, and the upper bound, 10.

Multidimensional Arrays

The arrays just discussed are considered one-dimensional arrays—only one number designates the location of an element of the array. The array is like a single row of data, but because there can be only one row, you don't have to worry about the row number—only the column number. For example, to retrieve the second element (Option Base 0) use myArray (1).

In some cases, a single dimension isn't enough. This is where multidimensional arrays come in. Where a one-dimensional array is a single row of data, a multidimensional array contains rows and columns.

0 0

Post a comment