Runtime error 9, subscript out of range

jrickards

New Member
Joined
Sep 15, 2014
Messages
12
I've looked up other posts on this error code but I still can't figure out my problem.

I've reduced my code to just a few lines.

Code:
Dim EastingArr As Variant
Dim k as Long
EastingArr = Workbooks("Area Calculations.xlsm").Worksheets("Sheet1").Range("Easting")
k = UBound(EastingArr)
MsgBox (k)
MsgBox (EastingArr(1))

I get the message box with k (18 is the number of elements in the spreadsheet and this is what I get from the message box) but then the error message appears. It appears that I can't access the items in the array.

Excel 2010
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
That array has two dimensions (row & column). Try:
MsgBox EastingArr(1,1)
 
Upvote 0
HAH!! That worked except that I don't want a 2-dimensional array, just a single dimension.

Assigning the values in a range to a variant results in a 2-D array even if the range is restricted to a single row or column, but if your range "Easting" is a single row or column you can convert the variant to a 1-D array.

For example if Easting is one column:
Dim oneD()
'code to create EastingArr here
Redim oneD(1 to Ubound(EastingArr,1))
for i = 1 to Ubound(EastingArr,1)
oneD(i) = EastingArr(i,1)
Next i
 
Upvote 0
Assigning the values in a range to a variant results in a 2-D array even if the range is restricted to a single row or column, but if your range "Easting" is a single row or column you can convert the variant to a 1-D array.

OK, I get it, it is like a single column in a spreadsheet. You can't address values in the column simply by the row number, even if there is only one column, you must use the column identifier. I'm fine with that, I don't need to go to the trouble of converting it to a 1D array.

Many thanks,

Jules
 
Upvote 0
OK, I get it, it is like a single column in a spreadsheet. You can't address values in the column simply by the row number, even if there is only one column, you must use the column identifier. I'm fine with that, I don't need to go to the trouble of converting it to a 1D array.

Many thanks,

Jules
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,797
Members
449,048
Latest member
greyangel23

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