sysuserwindows
New Member
- Joined
- Jan 16, 2022
- Messages
- 16
- Office Version
- 2019
- Platform
- Windows
- Mobile
- Web
In the code below, it appears an error message "subscript out of range" in the line "ReDim Preserve intA(0 To 3, 0 To 4)"
VBA Code:
Sub Populate2D()
'declare the 2D array
Dim intA() As Variant
'declare variables
Dim rw As Integer
Dim col As Integer
'initialize the array with 3 rows and 4 columns
ReDim intA(0 To 2, 0 To 3)
'populate the array
intA(0, 0) = 45
intA(0, 1) = 50
intA(0, 2) = 55
intA(0, 3) = 60
intA(1, 0) = 65
intA(1, 1) = 70
intA(1, 2) = 75
intA(1, 3) = 80
intA(2, 0) = 85
intA(2, 1) = 90
intA(2, 2) = 95
intA(2, 3) = 100
'redimension the array to 4 rows and 5 columns
ReDim Preserve intA(0 To 3, 0 To 4)
'populate the additional column with values for all four rows
intA(3, 4) = 45
intA(3, 4) = 50
intA(3, 4) = 55
intA(3, 4) = 60
'loop through the array and populate Excel
For rw = 0 To 3
For col = 0 To 4
Worksheets("Sheet2").Cells(rw + 1, col + 1).Value = intA(rw, col)
Next col
Next rw
End Sub