Array Question

dreid1011

Well-known Member
Joined
Jun 4, 2015
Messages
3,605
Office Version
  1. 365
Platform
  1. 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?
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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Whenever you pull a range straight into an array, the array will be 2D regardless or the number of rows/columns.
 
Upvote 0
Whenever you pull a range straight into an array, the array will be 2D regardless or the number of rows/columns.
In the case of my example, A1:A5, then, how would I reference the values without getting the subscript error?
 
Upvote 0
Exactly the same way as you do for a 2d array.
VBA Code:
Debug.Print arr(1, 1)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top