1

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
Community
  • 1
  • 1
adam
  • 414
  • 2
  • 8
  • 21
  • 3
    Possible duplicate of [How do I set up a "jagged array" in VBA?](https://stackoverflow.com/questions/9435608/how-do-i-set-up-a-jagged-array-in-vba) – A.S.H Jun 30 '17 at 03:31
  • 1
    @A.S.H - You should have found that **before** I wrote an answer! :D – YowE3K Jun 30 '17 at 03:34
  • 1
    @A.S.H, doh, me too. – Ambie Jun 30 '17 at 03:35
  • @YowE3K and vice-versa! :P anyway why delete your answer, I liked the "mixture". – A.S.H Jun 30 '17 at 03:35
  • @Ambie same... :) – A.S.H Jun 30 '17 at 03:36
  • @A.S.H I always worry about having an answer to a question that I then mark as "duplicate" - it seems like I'm just chasing rep points. (But I undeleted it anyway.) – YowE3K Jun 30 '17 at 03:37
  • @YowE3K everyone is chasing rep points, it's almost the only stimulator to write good answers isn't it? It's only about not overdoing :) – A.S.H Jun 30 '17 at 03:44
  • @A.S.H I'm not chasing rep points - the more points you have, the more SO housekeeping that you are expected to do - and I'm **lazy** so don't want to have to do it! ;-) – YowE3K Jun 30 '17 at 03:56
  • haha, thanks @A.S.H. jagged array, got to learn a new term today :) – adam Jul 03 '17 at 06:32

2 Answers2

3

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
YowE3K
  • 23,852
  • 7
  • 26
  • 40
3

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)
Ambie
  • 4,872
  • 2
  • 12
  • 26