ReDim error Subscript out of range

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
I have a sub (Sub1) that declare's a dynamic array as variant, activates a sheet then calls another sub (Sub2) that checks the size of a range in the activated sheet, ReDim's the array in two dimensions (first is range size, second is 2) and populates the array with values in the activated sheet. The first time this works just fine and the array values are correct.

Execution goes back to Sub1 which activates a second sheet and again calls Sub2. Sub2 checks the size of the next range, adds this to the size of the first range (the number is correct) and I think it should ReDim the array (ReDim Preserve) to the new larger first dimension size. However when I step through the code, before ReDim is executed the second time I get error '<= Subscript out of range'.

I can't understand what I'm doing wrong.
Any thoughts are appreciated.

Here's an excerpt of the VBA:

Sub GetNameTkr()

Dim NameTkrArray() As Variant
Dim NumRows As Integer

NumRows = 0

Workbooks("Stock Tickers.xlsm").Activate

Application.Goto (ActiveWorkbook.Sheets("NASDAQ").Range("A1"))
DataIntoArray NameTkrArray, NumRows

Application.Goto (ActiveWorkbook.Sheets("NYSE").Range("A1"))
DataIntoArray NameTkrArray, NumRows

End Sub

Sub DataIntoArray(NameTkrArray As Variant, NumRows)

Dim ArrayRowCounter As Integer

ArrayRowCounter = 1

NumRows = NumRows + Application.WorksheetFunction.CountA(Range("A:A"))

NumRows = NumRows - 1

ReDim Preserve NameTkrArray(NumRows, 2) As Variant 'THIS IS WHERE IT FAILS

Do Until ActiveCell.Offset(ArrayRowCounter, 0) = ""
NameTkrArray(ArrayRowCounter, 1) = ActiveCell.Offset(ArrayRowCounter, 1).Value 'puts corp name in array(x,1)
NameTkrArray(ArrayRowCounter, 2) = ActiveCell.Offset(ArrayRowCounter, 0).Value 'puts corp tkr in array(x,2)

ArrayRowCounter = ArrayRowCounter + 1

Loop

End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
When you use Redim Preserve you can only resize the last dimension, not the first one.
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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