Excel 如何在VBA中重复定义带有编号名称的数组?
我试图编写一个for循环,它重复定义数组,但每个循环的数组名不同。 例如:Excel 如何在VBA中重复定义带有编号名称的数组?,excel,vba,Excel,Vba,我试图编写一个for循环,它重复定义数组,但每个循环的数组名不同。 例如: for i = 1 to 10 dim array"i"(1 to 5) as long loop 在这里,我不知道如何将“I”转换为1,2,3…10,这样就有了array1,array2,array3…array10。关于如何工作有什么想法吗?数组数组 可能您需要按照Vityata的建议创建一个数组列表(也称为锯齿数组或数组数组数组),并执行sub调用来重新确定任何“sub”数组的尺寸 子调用标准化 Sub S
for i = 1 to 10
dim array"i"(1 to 5) as long
loop
在这里,我不知道如何将“I”转换为1,2,3…10,这样就有了array1,array2,array3…array10。关于如何工作有什么想法吗?数组数组 可能您需要按照Vityata的建议创建一个数组列表(也称为锯齿数组或数组数组数组),并执行sub调用来重新确定任何“sub”数组的尺寸 子调用
标准化
Sub Standardize(arr, lowerBoundary&, upperBoundary&)
' Purpose: change jagged array boundaries by preserving included items
' Note: argument arr is passed ByRef (by default)
Dim tmpArr
tmpArr = arr
ReDim Preserve tmpArr(lowerBoundary To upperBoundary)
arr = tmpArr
End Sub
测试显示在VBEditor的即时窗口中
* [4a] Display test items:
* NUMS - 6 items: 1,2,3,4,,
e.g. a(NUMS)(1) =1
e.g. a(NUMS)(2) =2
* ALPHA - 6 items: one,two,three,four,five,six
e.g. a(ALPHA)(5) =five
e.g. a(ALPHA)(6) =six
* [4b] Display first and last item of each category:
Nums: 1st item: |1|, last item: ||
Alpha: 1st item: |one|, last item: |six|
你不能。你到底想做什么?数组列表可能是最好的解决方案。为什么不使用二维数组呢
Dim Arr(1到10,1到5)
然后您可以调用它Arr(2,4)
,它返回第二个数组中的第四个项目。很好@Scott Craner,我会尝试一下,谢谢!甚至是数组的集合,这取决于您希望对数组执行的操作。
Sub Standardize(arr, lowerBoundary&, upperBoundary&)
' Purpose: change jagged array boundaries by preserving included items
' Note: argument arr is passed ByRef (by default)
Dim tmpArr
tmpArr = arr
ReDim Preserve tmpArr(lowerBoundary To upperBoundary)
arr = tmpArr
End Sub
* [4a] Display test items:
* NUMS - 6 items: 1,2,3,4,,
e.g. a(NUMS)(1) =1
e.g. a(NUMS)(2) =2
* ALPHA - 6 items: one,two,three,four,five,six
e.g. a(ALPHA)(5) =five
e.g. a(ALPHA)(6) =six
* [4b] Display first and last item of each category:
Nums: 1st item: |1|, last item: ||
Alpha: 1st item: |one|, last item: |six|