Subscript out of range

jonathan7272

New Member
Joined
Mar 12, 2011
Messages
20
Hi everyone,

I'm having such a nightmare with this. Could someone please explain what I'm doing wrong! I am wanting to redim an array, while preserving the existing data. I have simplified the kind of thing I'm trying to do in the code below. If I run this I get the error message "Run-time error '9': Subscript out of range".

Sub test()
Dim anArray() As String

ReDim anArray(10, 2)
For x = 0 To UBound(anArray)
anArray(x, 1) = x
Next x
ReDim Preserve anArray(20, 2)

For x = 0 To UBound(anArray)
Debug.Print anArray(x, 1)
Next x

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Used with Preserve, you can only change the last dimension of an array.​
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

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