adam
adam

Reputation: 424

How do i dim values in an array as arrays?

just out of curiosity.

is it possible to dim values in an array as their own individual array?

Sub test()

Dim ar(5) As Variant

ar(1) = a
ar(2) = b
ar(3) = c
ar(4) = d
ar(5) = e

For i = 1 To UBound(ar)
    Dim ar(i) As Variant '<---doesn't work :(
Next i


End Sub

Upvotes: 1

Views: 426

Answers (2)

Ambie
Ambie

Reputation: 4977

If you're after a matrix style array, then you could just define a multi-dimensional array:

Dim ar(5, x) As Variant

But it seems as though you want a jagged array, ie an array of arrays. In that case you just assign an Array() Variant to each element of your array:

Dim ar(5) As Variant
ar(0) = Array(1, 2, 3)

And the syntax to access the 'sub-elements' would be ar(x)(y):

Debug.Print ar(0)(1)

Upvotes: 3

YowE3K
YowE3K

Reputation: 23974

You should be able to set one (or more) position of a Variant array to be an array:

Sub test()

    Dim ar(5) As Variant
    Dim ar1(1 To 4) As Variant

    ar1(1) = 5
    ar1(2) = "x"
    Set ar1(3) = ActiveSheet
    ar1(4) = 10

    ar(1) = "a"
    ar(2) = "b"
    ar(3) = ar1
    ar(4) = "d"
    ar(5) = "e"

    Debug.Print ar(1)
    Debug.Print ar(3)(1)
    Debug.Print ar(3)(3).Name

End Sub

Upvotes: 3

Related Questions