ReDim arrays in Variant

P

Petr Danes

I have a syntax issue with VBA. It happens that I'm doing this in Access,
but the question applies to VBA all across the Office package, so I'm
posting this to several VBA-related groups, since it may also be of interest
to programmers in applications other than Access.

I have a variant which contains several one-dimensional arrays. I load each
array with some stuff I read out of the TAG property of a page, specific to
that page, so that the code will be able to handle an arbitrary number of
pages. I anticipate some growth, and coding it this way allows me to simply
add a new page, with the appropriate parameters in the TAG property and my
app will continue to function, without the necessity for altering the actual
code.

Fine so far, all this works well. My initial code was as follows:

Dim Kde(2)
..
..
Set Kde(1) = Array(0, 198, 0, 180, 1, 47)
Set Kde(2) = Array(0, 1980, 0, 1800, 1, 65)

and I referenced array elements with the standard syntax of

i = Kde(km)(0)
j = Kde(km)(5)

and so on.

What I want to do now is dim the variant without parameters,

Dim Kde()

and use ReDim to dynamically allocate BOTH the number of elements in the Kde
variant AND the number of elements in the individual arrays in the Kde
variant (not at the same time). As I scan for pages that interest me, I want
to use ReDim to add new elements to Kde when I find a page I like, then look
at the TAG property, count the parameters stored there and ReDim the array
in that newly added element of Kde to the proper number of elements before
loading the parameters.

As it is, I have to change the code to modify the number of elements when I
add a page. Using the ReDim would make the code completely modular, adding a
page would not then require ANY coding modifications. Does anyone know the
syntax for this? Is it even possible? The online help seems to indicate that
it can be done:

"Note To resize an array contained in a Variant, you must explicitly
declare the Variant variable before attempting to resize its array."

Conceptually, it also seems doable, but all my experiments with this have
yielded only syntax errors.

Petr
 
R

RB Smissaert

Try something like this:

Sub test()

Dim Kde(1 To 2)
Dim arr

Kde(1) = Array(0, 198, 0, 180, 1, 47)
Kde(2) = Array(0, 1980, 0, 1800, 1, 65)

MsgBox Kde(1)(UBound(Kde(1)))

arr = Kde(1)

ReDim Preserve arr(0 To 10)

Kde(1) = arr
Kde(1)(10) = 100

MsgBox Kde(1)(10)

End Sub


Note that you don't want to use Set as you did as that is for objects.


RBS
 
J

Joel

Sub test()
Dim Kde() As Variant
Dim Kde1 As Variant
Dim Kde2 As Variant
'..
'..
Kde1 = Array(0, 198, 0, 180, 1, 47)
Kde2 = Array(0, 1980, 0, 1800, 1, 65)


'and I referenced array elements with the standard syntax of
ReDim Kde(2)
Kde(0) = Kde1
Kde(1) = Kde2
i = Kde(km)(0)
j = Kde(km)(5)


End Sub
 
P

Per Jessen

Pi Petr,

You can only Redim the last dimension of your array, so to make it work as
you desire your data has to be transposed.

See if this example can help you:

Option Base 1
Sub Arrays()
Dim Kde()

ReDim Kde(5, 1)
tempArray = Array(0, 198, 0, 180, 1, 47)
For c = 1 To 5
Kde(c, 1) = tempArray(c)
Next
tempArray = Array(0, 1980, 0, 1800, 1, 65)
For Temp = 1 To 10
ReDim Preserve Kde(5, UBound(Kde, 2) + 1)
For c = 1 To 5
Kde(c, Temp) = tempArray(c)
Next
Next
End Sub

Check this site for further help:

http://www.cpearson.com/excel/VBAArrays.htm

Best regards,
Per
 
D

david

Public Sub myredim(v As Variant, x)
ReDim Preserve v(x)
End Sub

....

Dim Kde As Variant
ReDim Kde(2)
Kde(1) = Array(Null, 198, 0, 180, 1, 47)
Kde(2) = Array(0, 1980, 0, 1800, 1, 65)

ReDim Kde(3)
Kde(3) = Array(1)

myredim(Kde(2),6)
Kde(2)(6) = 7

(david)
 
G

Guest

Hello RB,

thank you, that was the way to do it. It didn't occur to me to use a
separate array and then assign it to an element in the variant, I was
trying to ReDim the array already contained in the variant, and there's
probably no syntax to do that directly. Your way works perfectly.

And of course, you're right about the Set. That wasn't a cut and paste from
my code, I retyped it into my post from (obviously faulty) memory. My actual
code has it right, otherwise it would not even have compiled.

Appreciate the help.

Petr
 
G

Guest

Hi Joel,

thank you, that works. I got a similar tip from RB, the temp array first,
then put that in the variant array. Works perfectly.

Petr
 
G

Guest

Hi Per,

thank you for the example. I got similar tips from RB and Joel, but using a
one dimensional variant. You use a two dimensional variant array, which is a
little more complicated than I needed in this case, but in all three cases,
the technique of using a temp array is the fundamental step which got me to
my goal.

And thank you also for the link to Pearson's site, there is a lot of useful
stuff there to study.

Petr
 
G

Guest

Thank you David, I've got it now.

Petr


david said:
Public Sub myredim(v As Variant, x)
ReDim Preserve v(x)
End Sub

...

Dim Kde As Variant
ReDim Kde(2)
Kde(1) = Array(Null, 198, 0, 180, 1, 47)
Kde(2) = Array(0, 1980, 0, 1800, 1, 65)

ReDim Kde(3)
Kde(3) = Array(1)

myredim(Kde(2),6)
Kde(2)(6) = 7

(david)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top