View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default 2 dimensional array (variant)

There is a method to do what the OP asked, but to address the "array" that
is produced requires the use of an odd syntax. Consider this...

Dim VariantArray As Variant
VariantArray = Array(Array(1, "h", 2), _
Array(4, "i", 5, "j", 6, "k", 1), _
Array(7, "z", 3, "y", 3))

Each internal Array function call defines a row in the "master" array; there
are variable number of columns in each row. Now, to address these, you would
use a double set of parentheses system to specify the index values. For
example, if you wanted to retrieve the "k" value above which is at "column"
6 of "row" 2; then, assuming the default Option Base of 0 (meaning
zero-based arrays), this is how you would do it...

Debug.Print VariantArray(1)(5)

To get the "y" from the 3rd row, 4th column, you would do this...

Debug.Print VariantArray(2)(3)

again, assuming zero-based arrays. Now, because each row has a different
upper bound, this is how you would query the array for the upper bound of
the array making up the 2nd row (again, assuming zero-based arrays)....

Debug.Print UBound(VariantArray(1))

That's it... looks screwy, but it does work.

Rick



"RB Smissaert" wrote in message
...
That doesn't look that smart to me as how are you loading the arrays that
you call array?
What are you trying to achieve or what particular problem are you trying
to solve?

RBS

"Tom Emmery" wrote in message
...
Is it possible to load the array in a smart way, like:
MyArray = array(1,"h", 2), array(4,"i",5,"j",6,"k",1),
array(7,"z",3,"y",3)

Would this work ?


"RB Smissaert" wrote:

Dim MyArray(1 To 3, 1 To 7)

MyArray(1, 1) = 1
MyArray(1, 2) = "h"
MyArray(1, 3) = 2
MyArray(2, 4) = 5

Msgbox MyArray(2, 4)


RBS

"Tom Emmery" wrote in message
...
How to create a 2-dimensional array, initialize it and read from it ?

Example:
fixed number of rows(3), variable number of values per row (max 6)

I've tried the following code:
dim MyArray as variant
redim MyArray(1 to 3, 1 to 7)

How to assign values to it ?
row 1: (1,"h", 2)
row 2: (4,"i",5,"j",6,"k",1)
row 3: (7,"z",3,"y",3)

Then how to select "j" (row 2, index 4) ?