Redim Question

th081

Board Regular
Joined
Mar 26, 2006
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Hi

I am trying to Redim a multi dimension array.

I have declared the array at the top of the module as

Dim DataStor as variant

Then in the sub i have a loop

For i = 1 to XYZ

r = r +1

Redim Preserve DataStor(r,16)

DataStor(r,1) = Unit
DataStor(r,2) = Time

etc



Next i

it picks up the values when r = 1 but on the next loop it says subscipt out of range. I am trying to get it to hold x rows and 16 columns of data to eventually transfer into a sheet
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You can only Preserve data when you change the size of the last (in this case second) dimension.

But you don't need to resize on every iteration of the loop. As soon as you have quantified XYZ you can:

Code:
ReDim DataStor(1 To XYZ, 1 To 16)
 
Last edited:
Upvote 0
Redim Preserve takes extra time and only works on the last dimension. Why not size or extra size (Redim) it first?
e.g. Redim DataStore(1 to XYZ, 1 to 16)

There are some tricks to Redim the rows and columns after it is filled. This shows how for rows. http://www.snb-vba.eu/VBA_Arrays_en.html#L_6.10
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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