Declaring Indexes on Arrays as Variables

Ed Song

Board Regular
Joined
Sep 1, 2014
Messages
90
I've been playing around with matrices in VBA and have been following some of the online tutorial services.
One tutorial lesson makes the student type the following code in a macro:

Dim MyArray(2, 3) As Integer

MyArray(0, 0) = 10

MyArray(0, 1) = 10
MyArray(0, 2) = 10
MyArray(0, 3) = 10

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

For i = 0 To 2

For j = 0 To 3

Cells(i + 1, j + 1).Value = MyArray(i, j)

Next j
Next i

When I run the program I get error message: i is an undefined variable. How do I define the index i and j variables without deleting the Option Explicit statement?
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Watch MrExcel Video

Forum statistics

Threads
1,109,522
Messages
5,529,328
Members
409,863
Latest member
stacy09
Top