如何将数组中的值调暗为数组?

时间:2017-06-30 03:03:39

标签: vba excel-vba excel

好奇只是出于好奇。

是否可以将数组中的值调暗为各自的数组?

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

2 个答案:

答案 0 :(得分:3)

您应该能够将Variant数组的一个(或多个)位置设置为数组:

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

答案 1 :(得分:3)

如果您正在使用矩阵样式数组,那么您可以定义一个多维数组:

Dim ar(5, x) As Variant

但似乎你想要一个锯齿状数组,即一个数组数组。在这种情况下,您只需为数组的每个元素分配一个Array() Variant:

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

访问“子元素”的语法是ar(x)(y)

Debug.Print ar(0)(1)