如何循环遍历二维数组的特定行?

How to loop through a specific row of a 2-dimensional Array?

我有二维数组,我想用 If-Then 语句检查特定行中的每个元素,并根据 If-Then 语句的结果为下一行赋值?遍历二维数组中一行元素的正确语法是什么?

遍历二维数组的一行

Option Explicit

Sub LoopThroughRow()
    
    Const RowIndex As Long = 2
    Const Criteria As Double = 3
    Const MinNum As Long = 1
    Const MaxNum As Long = 5
    
    ' Populate with random integers.
    
    Dim Data As Variant: ReDim Data(1 To 5, 1 To 5)
    
    Dim r As Long, c As Long
    
    For r = LBound(Data, 1) To UBound(Data, 1)
        For c = LBound(Data, 2) To UBound(Data, 2)
            Data(r, c) = Int((MaxNum - MinNum + 1) * Rnd + MinNum)
        Next c
    Next r
    
    ' Write criteria row.
    
    For c = LBound(Data, 2) To UBound(Data, 2)
        If Data(RowIndex, c) > Criteria Then
            Data(RowIndex + 1, c) = "Yes"
        Else
            Data(RowIndex + 1, c) = "No"
        End If
    Next c
    
    ' Print result.

    Debug.Print "Column", "Row " & RowIndex, "Row " & RowIndex + 1
    For c = LBound(Data, 2) To UBound(Data, 2)
        Debug.Print c, Data(RowIndex, c), Data(RowIndex + 1, c)
    Next c

End Sub

请尝试使用下一个 Sub:

Sub changeRow(arr As Variant, iR As Long, strTxt As String)
    Dim i As Long
    For i = LBound(arr, 2) To UBound(arr, 2) '(arr, 2) to determine the number of columns
        arr(iR, i) = arr(iR, i) & strTxt
    Next i
End Sub

当然,它可以设计为在相应的行上执行您需要的任何操作。连扩展参数都要用到。

可以通过以下方式轻松测试:

Sub testIterate2DArrayRow()
   Dim sh As Worksheet, arr, arrR, iRow As Long, strAdd As String
   
   Set sh = ActiveSheet
   iRow = 2          'the array row to be iterated
   strAdd = " - XX"  'string to be added to each row element (instructional example)
   arr = sh.Range("A2:D6").value           'the easiest way to create a 2D array
    arrR = Application.Index(arr, iRow, 0) 'create a 1D slice of the row to be iterated/modified
                                           'if you need only iterating to extract something, you may stop here
                                           'and iterate between its elements...
    Debug.Print Join(arrR, "|")            'just to visually see the row content
   changeRow arr, iRow, strAdd             'iterate on the iRow row (and modify something)
   Debug.Print Join(Application.Index(arr, iRow, 0), "|") 'visual evidence of the modification...
End Sub

已编辑:

我会把上面的代码让其他喜欢学习通用概念的人。

请测试下一个代码,它应该按照(我理解的)您需要的方式处理数组。

它的第一行只是创造了一个轻松检查概念的机会。因此,您应该将必要的间隔放在 Excel sheet 上,从“A1”到“J1”和 运行 以上代码。它将return处理后的数组从“L1”开始:

Sub analizeBays()
  Dim sh As Worksheet, BayRay(), i As Long
  
  Set sh = ActiveSheet
  BayRay = sh.Range("A1:J4").value 'only to easily test the concept
  
  For i = LBound(BayRay, 2) To UBound(BayRay, 2)
        If BayRay(1, i) <= 10 Then
           BayRay(2, i) = 2035
           BayRay(3, i) = 2005
           BayRay(4, i) = 1005
        ElseIf BayRay(1, i) > 10 And BayRay(1, i) <= 12 Then
           BayRay(2, i) = 2022
           BayRay(3, i) = 1032
           BayRay(4, i) = 4344
        End If
  Next i
  'drop the processed array content starting from "L1")
  sh.Range("L1").Resize(UBound(BayRay), UBound(BayRay, 2)).value = BayRay
End Sub