如何获得二维数组中Element的索引?

问题描述 投票:0回答:4

我有一个二维的数组。我也有一个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
arrays excel vba indexing dimensions
4个回答
1
投票

显示2维数组中元素的索引-复杂的方法

如果我理解正确,您正在通过►For Each构造遍历数据字段数组,并希望获取同一数组的当前行/列索引对。

为了回答您的问题

“如何获得二维数组中元素的索引

I leave aside如果您通过先循环遍历数组行并在此循环内最终遍历数组列来更改逻辑,则会以更明显和通常的方式自动获得这些- [附录 *)

为了进行例如下面的示例调用中的第6个数组元素引用当前索引对(元素i=6〜> table1(3,2)〜> row:= 3 / column:= 2),将很有必要

每次获得下一个元素时,通过将其值增加+1来添加元素计数器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 enter image description here

    *)附录-“简单方法”

    正相反,如上所述,使用行和列变量rc;允许仅通过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

    1
    投票
    [当您使用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

    您可以计算数组元素的行和列。并且代码证明使用它们,返回的数组值恰好是找到的数组值...

    在数组'索引'上有更多亮点了吗??

    0
    投票
    表1是变量(1到33,1到9)Table2是Variant(1到33,1到1)
    此33和9是动态的。

    0
    投票
    © www.soinside.com 2019 - 2024. All rights reserved.