Adding another dimension to an array and preserving

mountainman88

Board Regular
Joined
Jun 22, 2019
Messages
109
Office Version
  1. 2016
Platform
  1. Windows
So I have an array called Setup declared as a variant.

i pass a range to the array eg A1:E7 and Setup becomes an array like this

setup (1 to 7, 1 to 5)

i want add another dimension (1 to 3) but preserve the range data already in the array. What’s the best way to do this?

I know I can’t ReDim and add a dimension. So I try to declare a new array to copy the old Setup array to using a loop, but the dimensions are based on a variable range and you can’t declare an array with variables?

thanks this forum is awesome
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
try this:
Code:
Sub test()
Dim newarray
initarray = Range("a1:e7")
d1 = UBound(initarray, 1)
d2 = UBound(initarray, 2)
ReDim newarray(1 To d1, 1 To d2, 1 To 3)
For i = 1 To d1
 For j = 1 To d2
  newarray(i, j, 1) = initarray(i, j)
 Next j
Next i
MsgBox ("done")


End Sub
 
Last edited:
Upvote 0
Hey thanks,

I can’t try this just yet but already think it won’t work because you’re using variables to Redim an array?

I also just read that if I’m using 3D arrays then there must be an easier way to do it. Maybe with collections which I don’t understand yet?
 
Last edited:
Upvote 0
What are you going to use the extra dimension for?
 
Upvote 0
It’s going to hold three possible strings (totals, data, blank) based on conditions applied to the other two dimensions
 
Upvote 0
I can’t try this just yet but already think it won’t work because you’re using variables to Redim an array?
You would be incorrect about that... you can ReDim with variables, what you cannot use variables for is Dim.
 
Upvote 0
You would be incorrect about that... you can ReDim with variables, what you cannot use variables for is Dim.

Well dang if I knew that I would have figured it out!

So is the usual workaround to add a dimension and ‘preserve’ the contents of an array? Create a new array, Redim it with variables and copy over from the old array using a loop?


Thanks again
 
Last edited:
Upvote 0
Yes I know and thanks for your code!

I was wondering if this is the ‘typical’ method?
 
Upvote 0
It is certainly the way I would do it. I can't actually think of another way of doing it!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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