Multidimensional arrays

The arrays created in the previous examples are all one-dimensional arrays. Arrays you create in VBA can have as many as 60 dimensions — although you rarely need more than 2 or 3 dimensions in an array. The following example declares a 100-integer array with two dimensions:

Dim MyArray(1 to 10, 1 to 10) As Integer

You can think of this array as occupying a 10-x-10 matrix. To refer to a specific element in this array, you need to specify two index numbers. The following example shows how you can assign a value to an element in this array:

This statement assigns a value to a single element in the array. If you're thinking of the array in terms of a 10-x-10 matrix, this assigns 125 to the element located in the third row and fourth column of the matrix.

You can think of a three-dimensional array as a cube. Visualizing an array of more than three dimensions is more difficult. Sorry, I haven't yet mastered the fourth dimension and beyond.

0 0

Post a comment