我有一个二维数组。 我也有一个For Each循环,该循环与这些数组的元素循环。
在代码中发表评论时,如何获取vElement / vElement2的索引? 如果您能帮助我,我将非常感谢。
For Each vElement In Table1
For Each vElement2 In Table2
If ws_1.Cells(1, c) = vElement Then
For Row = 3 To lastRow
amountValue = amountValue + ws_1.Cells(Row, c).value
ws_2.Cells(row2, colIlosc) = amountValue
'Here i would love to have index of vElement for example. In my head it would be something like... Index(vElement) or Index(Table1(vElement))
ws_2.Cells(row2, columncodeprod) = vElement2
row2 = row2 + 1
amountValue = 0
Next Row
End If
Next vElement2
Next vElement
答案 0 :(得分:1)
您进行讨论的情况下没有索引...
vElement
和vElement2
变量是Variant类型。它们不是具有Index
属性的对象。
使用For Each vElement In Table1
循环时,VBA从数组的第一个元素开始,向下直到最后一行,然后对下一列进行同样的操作。
当您需要知道如何命名数组“索引”时,必须使用For i = 1 To Ubound(Table1, 1)
,后跟For j = 1 To Ubound(Table1, 2)
。在这种情况下,您将知道匹配的数组元素的行和列。我们可以将它们视为您的伪索引...
如果您确实希望/坚持在For Each vElement In Table1
类型的迭代中提取此类索引,则必须构建它们。我将尝试使用elocvent代码示例:
Sub testElemIndex()
Dim sh As Worksheet, Table1 As Variant, vElement As Variant
Dim i As Long, indexRow As Long, indexCol
Set sh = ActiveSheet
sh.Range("C6").value = "TestIndex"
Table1 = sh.Range("A1:E10").value
For Each vElement In Table1
i = i + 1:
If vElement = "TestIndex" Then
If i <= UBound(Table1, 1) Then
indexRow = i: indexCol = 1
Else
indexCol = Int(i / UBound(Table1, 1)) + 1
indexRow = i - Int(i / UBound(Table1, 1)) * UBound(Table1, 1)
End If
Debug.Print Table1(indexRow, indexCol), indexRow, indexCol: Stop
End If
Next
End Sub
您可以计算数组元素的行和列。并且代码证明使用它们,返回的数组值恰好是找到的值...
对数组“索引”有更多了解吗?
答案 1 :(得分:1)
显示2维数组中元素的索引-复杂的方式
如果我理解正确,那么您正在通过►{For Each
构造遍历数据字段数组,并希望获取同一数组的当前行/列索引对。
为了回答您的问题
“如何获取二维数组中元素的 indices ”,
我抛开,如果您通过先循环遍历数组行并最终在循环内遍历来更改逻辑,则将以更明显和通常的方式自动获取这些数组列-请参见附录 *)
。
允许进行例如下面的示例调用中的第6个数组元素引用当前索引对(元素i=6
〜> table1(3,2)
〜> row:= 3 / column:= 2),将很有必要
i
getIndex()
将结果作为另一个数组返回,即仅包含两个值的数组:(1)当前数组行,(2)当前数组列:
示例呼叫
注意: 为了更好的可读性以及为了将答案压缩为所需的最低要求(参见MCVE),以下示例调用仅执行一个For Each
循环在table1
数据字段数组上;您将可以根据自己的需要对此进行更改或提出其他问题。
Option Explicit ' declaration head of your code module
Sub ShowIndicesOf2DimArray()
Dim table1 ' declare variant 1-based 2-dim datafield
table1 = Sheet1.Range("A2:B4") ' << change to sheets Code(Name)
Dim vElem, i As Long
Dim curRow As Long, curCol As Long ' current row/column number
For Each vElem In table1
i = i + 1 ' increment element counter
curRow = getIndex(table1, i)(1) ' <~ get row index via help function
curCol = getIndex(table1, i)(2) ' <~ get col index via help function
'optional debug info in VB Editors immediate window (here: Direktbereich)
Debug.Print i & ". " & _
" Table1(" & curRow & "," & curCol & ") = " & vElem & vbTab;
Debug.Print ", where curRow|curCol are " & Join(getIndex(table1, i), "|")
Next vElem
End Sub
上述过程调用的帮助功能getIndex()
Function getIndex(table1, ByVal no As Long) As Variant
'Purpose: get 1-based 1-dim array with current row+column indices
ReDim tmp(1 To 2)
tmp(1) = (no - 1) Mod UBound(table1) + 1
tmp(2) = Int((no - 1) / UBound(table1) + 1)
getIndex = tmp
End Function
*)
附录-“简单方法”
相反,如上所述,使用行和列变量r
和c
来绕行;允许仅通过table1(r,c)
引用项目:
Sub TheSimpleWay()
Dim table1 ' declare variant 1-based 2-dim datafield
table1 = Sheet1.Range("A2:B4") ' << change to sheets Code(Name)
Dim vElem, i As Long
Dim r As Long, c As Long ' row and column counter
For r = 1 To UBound(table1) ' start by row 1 (1-based!) up to upper boundary in 1st dimension
For c = 1 To UBound(table1, 2) ' start by col 1 (1-based!) up to upper boundary in 2nd dimension
i = i + 1
Debug.Print i & ". " & _
" Table1(" & r & "," & c & ") = " & table1(r, c) & vbTab;
Debug.Print ", where row|col are " & r & "|" & c
Next c
Next r
End Sub
答案 2 :(得分:0)
Dim Table1() As Variant
Dim Table2() As Variant
Table1 = Range(Cells(2, 3), Cells(lastRow, vMaxCol))
Table2 = Range(Cells(2, 1), Cells(lastRow, 1))
表1是变量(1到33,1到9) Table2是Variant(1到33,1到1)
这33和9是动态的。