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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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