将数组存储在数组中

在Excel VBA中,有没有办法将数组存储在另一个数组中? 例如,如果我创build了一个名为“World”的一维数组和具有不同名称的二维数组,那么是否可以将每个2D数组存储到“World”的每个项目中(不pipe长度如何,数组“World” )? 如果是这样,我怎么能这样做,我怎么会参考“世界”内的二维数组中的项目?

我应该看看使用对象和/或类吗? 你如何做到这一点,或者你有一个很好的地方参考我? 我一直在网上查找一段时间,还没有find解决scheme。 任何帮助是极大的赞赏。

在我看来,我会用一个集合。 然后你可以有collections的集合。 集合是好的,因为你可以参考“钥匙”,并获得相应的价值… … –

  Public Function MultiDimensionalCollection() as Collection Dim tempColl as Collection Set MultiDimensionalCollection = new Collection For i = 1 to 100 Set tempColl = New Collection tempColl.Add "Value", "Key-" & i tempColl.Add "Value2", "Key2-" & i tempColl.Add "Value3", "Key3-" & i MultiDimensionalCollection.Add tempColl, "Key-" & i Next i End Function 

你显然可以用任何东西(对象,范围值等)填充它们。 只要确保不要复制“KEY”值,否则会出错。 让我知道,如果你想从范围或logging集或任何你想要的样品代码填补他们。 谢谢,Brian。

你可能可以使用一个3d数组,或称之为Jagged数组 (或数组数组)

 Sub ThreeDArray() Dim World() As String ' or type of your choice ReDim World(0 To 4, 1 To 3, 1 To 2) World(5, 1, 2) = "a" Debug.Print World(5, 1, 2) End Sub 

 Sub JaggedArray() Dim World() As Variant Dim MyArray() As String ' or type of your choice Dim i As Long, j As Long ' If all elements of World are the same size ReDim World(0 To 9) ReDim MyArray(1 To 2, 1 To 3) For i = LBound(World) To UBound(World) World(i) = MyArray Next ' Or, if each element of World is different size ReDim World(0 To 9) For i = LBound(World) To UBound(World) ReDim MyArray(0 To i, 0 To (i + 1) * 2) World(i) = MyArray Next ' to access elements World(5)(1, 2) = "a" Debug.Print World(5)(1, 2) End Sub