How to add a dimension to convert a 1D array into a 2D array?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
I start with a 1D array, e.g., Array1(1 to 24), and would like to add a dimension and convert this to a 2D array, e.g., Array1(1 to 24, 1 to 36), while retaining the original values from the 1D array.

I tried the following, but the VBA routine exits with an error:

ReDim Preserve Array1(1 to 24, 1 to 36)

Is VBA unable to add extra dimensions to an existing array, or am I using the wrong method?
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
When using Preserve in a ReDim, you can only change the size of the last dimension, you can't change the size of other dimensions, or add dimensions. If you know that you're going to add a dimension later, just define it originally as 1 To 1 like this:

Code:
Sub test1()
Dim array1()


    ReDim array1(1 To 24, 1 To 1)
    
    array1(1, 1) = 5
    
    ReDim Preserve array1(1 To 24, 1 To 36)
    
    Debug.Print array1(1, 1)
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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