Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.access.modulescoding,microsoft.public.excel.programming,microsoft.public.word.programming,microsoft.public.word.vba.general
external usenet poster
 
Posts: 1,533
Default ReDim arrays in Variant

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

"Petr Danes" skrev i meddelelsen
...
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



--
This e-mail address is fake, to keep spammers and their address harvesters
out of my hair. If you need to get in touch personally, I am 'pdanes' and
I use yahoo mail. But please use the newsgroups whenever possible, so that
all may benefit from the exchange of ideas.



  #2   Report Post  
Posted to microsoft.public.access.modulescoding,microsoft.public.excel.programming,microsoft.public.word.programming,microsoft.public.word.vba.general
No Name
 
Posts: n/a
Default ReDim arrays in Variant

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



"Per Jessen" píše v diskusním příspěvku
...
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

"Petr Danes" skrev i meddelelsen
...
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



--
This e-mail address is fake, to keep spammers and their address
harvesters out of my hair. If you need to get in touch personally, I am
'pdanes' and I use yahoo mail. But please use the newsgroups whenever
possible, so that all may benefit from the exchange of ideas.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays/Redim/Help! LarryP Excel Programming 2 June 2nd 08 06:04 PM
ReDim an array of variant - Help doj Excel Programming 1 October 27th 06 11:00 PM
ReDim Variant array for worksheet copy John Keith[_2_] Excel Programming 1 February 16th 06 09:52 PM
how to redim the type of variant element lvcha.gouqizi Excel Programming 5 October 25th 05 07:05 PM
ReDim, Preserve and Multidimensional arrays Andy Westlake[_2_] Excel Programming 3 October 19th 04 07:04 PM


All times are GMT +1. The time now is 07:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright Š2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"