我似乎无法在任何地方找到此问题。
我需要声明一堆动态数组如下:
Dim list1 () as variant
Dim list2() as variant
Dim list3() as variant
...
Dim listN() as Variant
每个列表都是一维动态数组。但是,我不知道" N"将在该计划期间。我想制作这些" N"列出动态。我尝试过二维动态数组。但是" redim"声明要求同时声明两个维度。特别是,我这样做:
Dim BigList() as variant
...
Redim BigList(listNum, listLength)
访问/传入子" list1"," list2" ," list3" ...,调用" BigList(1)"," BigList(2)"给我错误。特别是在我的代码中的某个地方,有这一部分:
sub ProcessList(byref listToProcess() as variant)
...
end sub
sub main()
...
call ProcessList(list1)
call ProcessList(list2)
...
call ProcessList(listN)
end sub
Now I can do a loop:
for i = 1 to N
Call ProcessList(list"i")
next i
这需要列表" i"成为一维动态数组。因此,在redim BigList(listNum,listLength)之后,我执行此操作:
for i = 1 to N
Call ProcessList(BigList(i)) 'i refers to listNum
next i
这给了我错误"不兼容的类型"。
答案 0 :(得分:1)
以下是创建Dictionary
的一个示例,该N
键入整数值(即ExtendList
),每个值最初都是空数组。
然后,您可以使用类似Sub foo()
Dim BigList As Object
Dim N As Long
Dim v as Variant
'Create an empty dictionary object
Set BigList = CreateObject("Scripting.Dictionary")
'Add N empty array to the dictionary:
N = 3
For i = 1 To N
BigList(i) = Array()
Next
'Resize one of the items in your BigList
BigList(2) = ExtendList(BigList(2), 1, 10)
v = BigList(2) 'Here you can examine v in the Locals window and see it is an array, of dimensions 1 x 10
End Sub
Function ExtendList(lst, a As Long, b As Long)
ReDim lst(a, b)
ExtendList = lst
End Function
函数的内容根据需要调整这些空数组的大小。
ReDim
在审核您编辑的问题时,我认为您只是误解了Redim BigList(listNum, listLength)
声明的工作原理:
BigList
这会根据参数listNum
和listLength
重新标注BigList
。它确实不(看起来你可能已经预料到)在 ReDim Preserve BigList(listNum)
BigList(listNum) = Array()
ReDim BigList(listNum)(listSize)
中创建一个数组列表。
我认为这也可行(未经测试,并且记住数组是零索引):
C11