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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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