I'm using VB6 and I need to do a ReDim Preserve to a Multi-Dimensional Array:
Dim n, m As Integer
n = 1
m = 0
Dim arrCity() As String
ReDim arrCity(n, m)
n = n + 1
m = m + 1
ReDim Preserve arrCity(n, m)
Whenever I do it as I have written it, I get the following error:
runtime error 9: subscript out of range
Because I can only change the last array dimension, well in my task I have to change the whole array (2 dimensions in my example) !
Is there any workaround or another solution for this?
You can use a user defined type containing an array of strings which will be the inner array. Then you can use an array of this user defined type as your outer array.
Have a look at the following test project:
Run the project, and click on the form to display the contents of the arrays.
Click on Command1 to enlarge the outer array, and click on the form again to show the results.
Click on Command2 to enlarge an inner array, and click on the form again to show the results.
Be careful though: when you redim the outer array, you also have to redim the inner arrays for all the new elements of the outer array
Since VB6 is very similar to VBA, I think I might have a solution which does not require this much code to
ReDim
a 2-dimensional array - usingTranspose
.The solution (VBA):
What is different from OP's question: the lower bound of
arrCity
array is not 0, but 1. This is in order to letApplication.Transpose
do it's job.I think you should have the
Transpose
method in VB6.This is more compact and respect the intial first position in array and just use the inital bound to add old value.
I call this sub with this line to resize the first dimension
You can add an other test to verify if the initial size is not upper than new array. In my case it's not necessary
In regards to this:
Just use a jagged array (ie an array of arrays of values). Then you can change the dimensions as you wish. A bit more work perhaps, but a solution.
I stumbled across this question while hitting this road block myself. I ended up writing a piece of code real quick to handle this
ReDim Preserve
on a new sized array (first or last dimension). Maybe it will help others who face the same issue.So for the usage, lets say you have your array originally set as
MyArray(3,5)
, and you want to make the dimensions (first too!) larger, lets just say toMyArray(10,20)
. You would be used to doing something like this right?But unfortunately that returns an error because you tried to change the size of the first dimension. So with my function, you would just do something like this instead:
Now the array is larger, and the data is preserved. Your
ReDim Preserve
for a Multi-Dimension array is complete. :)And last but not least, the miraculous function:
ReDimPreserve()
I wrote this in like 20 minutes, so there's no guarantees. But if you would like to use or extend it, feel free. I would've thought that someone would've had some code like this up here already, well apparently not. So here ya go fellow gearheads.
I haven't tested every single one of these answers but you don't need to use complicated functions to accomplish this. It's so much easier than that! My code below will work in any office VBA application (Word, Access, Excel, Outlook, etc.) and is very simple. Hope this helps: