Excel VBA Programming - Multidimensional Arrays
Excel VBA Programming - Multidimensional Arrays
Home and Excel VBA
Learn
Multidimensional
Arrays in Excel VBA
The arrays in our last lesson were all one dimensional arrays. It's one
dimensional because there's only one column of items. But you can have arrays
with more than one dimension. In theory you could have an array with up to 60
dimensions, in Excel VBA. However, you'll be glad to know that it's a rare
programme that uses more than 3 dimensions, or even 2 dimensions.
To set up an array with more than one dimension, you simply add a comma
after the 韈�rst number between the round brackets of your array name, then add
another number. Like this:
Or like this:
https://www.homeandlearn.org/multidimensional_arrays.html 1/7
21/03/2021 Excel VBA Programming Multidimensional Arrays
In the second declaration above, we've speci韈�ed that the array positions should
start at 1 rather than the default 0.
The arrays above are both 2-D arrays. If you want to add another dimension, just
add another comma and another number:
In this next exercise, we'll set up a 2-D array. We'll then print out arrays values in
cells on the spreadsheet.
Create a new Sub and call it ArrayExercise_3. (You can use your spreadsheet
from the previous lesson, if you like.) As the 韈�rst line of code, add this line:
This sets up a 2-D array. Think of this like the rows and columns on your
spreadsheet. The 2 means 3 rows (0 to 2, remember). The 3 means 4 columns.
MyArray(0, 0) = 10
MyArray(0, 1) = 10
MyArray(0, 2) = 10
MyArray(0, 3) = 10
This means row 0 column 0 has a value of 10, row 0 column 1 has a value of 10,
row 0 column 2 has a value of 10, and row 0 column 3 has a value of 10.
https://www.homeandlearn.org/multidimensional_arrays.html 2/7
21/03/2021 Excel VBA Programming Multidimensional Arrays
MyArray(1, 0) = 20
MyArray(1, 1) = 20
MyArray(1, 2) = 20
MyArray(1, 3) = 20
MyArray(2, 0) = 30
MyArray(2, 1) = 30
MyArray(2, 2) = 30
MyArray(2, 3) = 30
The new lines add values to the rest of the positions in the array.
To go through all positions in a 2-D you need a double loop. A double loop
means one loop inside another. The outer loop takes care of the rows while the
inner loop takes care of the columns. (The rows are the 韈�rst positions between
the round brackets of MyArray, while the column are the second positions
between the round brackets of MyArray)
For i = 0 To 2
Next i
For i = 0 To 2
For j = 0 To 3
https://www.homeandlearn.org/multidimensional_arrays.html 3/7
21/03/2021 Excel VBA Programming Multidimensional Arrays
Next j
Next i
The variable for the inner loop is j rather than i. But they are just variable names,
so we could have called them almost anything we liked. Notice, too, that the
outer loop goes from 0 to 2 while the inner loop goes from 0 to 3. These equate
to the numbers between round the brackets of MyArray when we set it up.
The code for the loop is this, but it needs to go between the For and Next of the
inner loop:
This is quite complex, so we'll go through it. Take a look at the Cells part:
Cells(i + 1, j + 1)
Cells(0, 0)
In case you're wondering why the 韈�rst time round the loop would produce
values of 0, 0 for Cells, here's an explanation.
The 韈�rst line in the outer loop is another loop. This means that the entire inner
loop will execute from 0 to 3. VBA will then drop to the Next i line. The next i
https://www.homeandlearn.org/multidimensional_arrays.html 4/7
21/03/2021 Excel VBA Programming Multidimensional Arrays
after 0 is 1. The end condition for the outer loop, however, is 2, so we're not
done with the outer loop yet. So again it drops down to execute its code. Its code
just happens to be the inner loop, so it executes the whole of this inner loop
again. In other words, the outer loop is going round and round from 0 to 2
times. As it's going round and round, it just so happens that it will run the inner
loop 0 to 3 times.
The 韈�rst time round, the values in the inner loop will be:
0, 0
0, 1
0, 2
0, 3
The second time round the inner loop, values for i and j will be:
1, 0
1, 1
1, 2
1, 3
2, 0
2, 1
2, 2
2, 3
So the 韈�rst number, which is i, goes up by 1 each time. These are the rows. The
second number, j, will always be 0, 1, 2 and then 3 (the columns).
Notice that after the equal sign of the Cells line, we have this:
= MyArray(i, j)
The i and j between the round brackets of MyArray will be the same as the
numbers above.
https://www.homeandlearn.org/multidimensional_arrays.html 5/7
21/03/2021 Excel VBA Programming Multidimensional Arrays
Run the code and see what happens. Switch back to your spreadsheet and you
should see this:
In the next lesson, we'll take a look at the Split function and how it relates to
arrays.
Lots more free online courses here on our main Home and Learn site
https://www.homeandlearn.org/multidimensional_arrays.html 6/7
21/03/2021 Excel VBA Programming Multidimensional Arrays
https://www.homeandlearn.org/multidimensional_arrays.html 7/7