dreid1011
Well-known Member
- Joined
- Jun 4, 2015
- Messages
- 3,633
- Office Version
- 365
- Platform
- Windows
I'm just poking around with arrays to refresh myself since it's been a while, but perhaps someone could answer this question.
When I set the array to a 2d range (H1:I2), it works. When I loop through to set the 1d array, it works. When I try to set the 1d array similar to the 2d array, I get subscript out of range errors when I try to print the values. Can I not set a 1d array with a range like this?
When I set the array to a 2d range (H1:I2), it works. When I loop through to set the 1d array, it works. When I try to set the 1d array similar to the 2d array, I get subscript out of range errors when I try to print the values. Can I not set a 1d array with a range like this?
VBA Code:
arr() = ws.Range("A1:A5").Value
Debug.Print arr(1)
VBA Code:
Private Sub ArrayTestI()
Dim ws As Worksheet: Set ws = Sheets("Array Testing Grounds")
Dim lRow, i&: lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
Dim arr() As Variant: ReDim arr(lRow)
'This does not work as expected.
arr() = ws.Range("A1:A5").Value
Debug.Print arr(1)
'This works as expected.
'arr() = ws.Range("H1:I2").Value
'Debug.Print arr(1, 1)
'Debug.Print arr(1, 2)
'Debug.Print arr(2, 1)
'Debug.Print arr(2, 2)
'This also works as expected.
'For i = 1 To lRow
' arr(i - 1) = ws.Range("A" & i).Value
' Debug.Print arr(i - 1)
'Next i
End Sub