Redim Jagged Arrays VBA

时间:2015-03-04 22:36:01

标签: arrays vba jagged-arrays

关于锯齿状阵列的快速问题。我有一个不会改变大小的静态容器数组:

Dim StaticArray(1 to 3, 1 to 4, 1 to 12) as variant

我正在为静态数组中的每个索引分配数组值,如下所示:

Dim ArrayInput() as Variant
ArrayInput = Array(1,2,3,4,5)
StaticArray(1,1,1) = ArrayInput

在将值数组分配到StaticArray之后,我希望能够灵活地向ArrayInput变量添加一个值。

有没有办法重新保存StaticArray(1,1,1)中包含的Variant?类似的东西:

Redim Preserve StaticArray(1 to 3, 1 to 4, 1 to 12)(1 to ubound(?)+1)

或者是修改ArrayInput变量并重新读取的唯一选项吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我想知道同样的事情!

正如您所提到的,我只能通过创建临时数组,重新定标然后重新分配到原始锯齿状数组来实现此目的。这是我的代码:

Dim StaticArray(1 To 3, 1 To 4, 1 To 12) As Variant
Dim ArrayInput() As Variant
Dim TempArray() As Variant

ArrayInput = Array(1, 2, 3, 4, 5)
StaticArray(1, 1, 1) = ArrayInput

'Instead of redim, store array temporarily
TempArray = StaticArray(1, 1, 1)

'Redim the temporary
ReDim Preserve TempArray(UBound(TempArray) + 1)

'Asign value
TempArray(UBound(TempArray)) = 6

'Then store again on statick array
StaticArray(1, 1, 1) = TempArray