[英]How to add loop counter value as array name suffix
I have 3 variant arrays: Array1 Array2 Array3 我有3个变量数组:Array1 Array2 Array3
I am wanting to add the counter value of my for loop to the end of the array name to save repeating the same code line over eg : 我想将我的for循环的计数器值添加到数组名称的末尾,以节省在例如上重复相同的代码行:
For i = 1 To oXlWkBk.Sheets.Count
FillArray ArraySource & i, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Next i
instead of: 代替:
For i = 1 To oXlWkBk.Sheets.Count
FillArray ArraySource1, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
FillArray ArraySource2, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
FillArray ArraySource3, "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Next i
I've tried using: 我试过使用:
Is it possible to append the counter value as a suffix to complete the array name? 是否可以将计数器值添加为后缀以完成数组名称?
This should work: 这应该工作:
Dim Arrays As Variant
Arrays = Array(Array1, Array2, Array3)
For i = 1 To oXlWkBk.Sheets.Count
FillArray Arrays(i-1), "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Next i
Since Array()
returns a 0-based array you need to be careful with the indices. 由于
Array()
返回基于0的数组,因此您需要小心使用索引。
Can you try to store all three arrays inside an array and then reference the containing array with the i
? 您可以尝试将所有三个数组存储在一个数组中,然后用
i
引用包含的数组吗? For example, 例如,
Sub test()
largerArray = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9))
For i = 0 To 2
Debug.Print (sumArray(largerArray(i)))
Next i
End Sub
Function sumArray(thisArray As Variant) As Integer
sumArray = 0
For i = LBound(thisArray) To UBound(thisArray)
sumArray = sumArray + thisArray(i)
Next i
End Function
So in your case, you would have 因此,就您而言,
largerArray=Array(ArraySource1, ArraySource2, ArraySource3)
for i=0 to oXlWkBk.Sheets.Count
FillArray largerArray(i), "B1:" & oCurrentWs.Cells(lNumRows, lNumCols).Address(RowAbsolute:=False, ColumnAbsolute:=False)
etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.