微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 For each Statement 在 VBA Excel 中循环遍历多维数组 - 仅限第一维

如何解决使用 For each Statement 在 VBA Excel 中循环遍历多维数组 - 仅限第一维

我想知道是否可以遍历多维数组并将循环限制为仅第一维。

我想出了一个例子来说明我的观点:

我使用我在 excel 中的数据来创建一个矩阵,我希望循环只打印与索引列对应的值。我尝试了不同的方法,但似乎没有任何效果,除非使用 if 条件退出 for 循环。

Data table

这是原始代码

Public matrix1() As Variant

Sub elements_in_loop()

Range("A3").Select
Range(Selection.End(xlToRight),Selection.End(xlDown)).Select
matrix1 = Selection

For Each Record In matrix1

Debug.Print Record

Next

End Sub

这是带有 if 条件的代码

Public matrix1() As Variant

Sub elements_in_loop()

Range("A3").Select
Range(Selection.End(xlToRight),Selection.End(xlDown)).Select
matrix1 = Selection

For Each Record In matrix1

    If Record <= UBound(matrix1) Then

        Debug.Print Record
    
    Else:
    
        Exit For
    
    End If

Next

End Sub

非常感谢有关如何在没有 if 条件的情况下执行此操作的建议。

更新:

我需要此信息的目的是在两个矩阵中查找相似的记录而不会出现“类型不匹配”错误

这是我使用的数据:

黄色的记录是在两个矩阵中的记录。

data updated

这是我为获取匹配记录而编写的代码,但仍在使用 if 条件将搜索限制为仅第一维。

Public matrix1() As Variant
Public matrix2() As Variant

Sub elements_in_loop()

Range("A3").Select
Range(Selection.End(xlToRight),Selection.End(xlDown)).Select
matrix1 = Selection

Range("G3").Select
Range(Selection.End(xlToRight),Selection.End(xlDown)).Select
matrix2 = Selection

For Each Record In matrix1

    If Record <= UBound(matrix1) Then
    
            For Each Record2 In matrix2
            
                If Record2 <= UBound(matrix2) Then
                        
                        If matrix1(Record,4) = matrix2(Record2,4) Then
                        
                                Debug.Print "There's match"
                                Debug.Print Record
    
                        Else: End If
                Else:
                
                        Exit For
                        
                End If
                
            Next
            
    Else:
    
        Exit For
    
    End If

Next


End Sub

解决方法

只需使用常规 for 循环并循环第一个维度:

Sub elements_in_loop()
    With ActiveSheet
        matrix1 = .Range(.Range("A3").End(xlToRight),.Range("A3").End(xlDown))
        matrix2 = Range(Range("G3").End(xlToRight),Range("G3").End(xlDown))
        
        Dim i As Long
        For i = LBound(matrix1,1) To UBound(matrix1,1)
            Dim j As Long
            For j = LBound(matrix2,1) To UBound(matrix2,1)
                If matrix1(i,4) = matrix2(j,4) Then
                    Debug.Print "There's match"
                    Debug.Print matrix1(i,4)
                End If
            Next j
        Next i
    End With
End Sub
,

另一种方法是直接使用范围

查看如何设置引用并遍历第一列单元格

Public Sub elements_in_loop()
    
    ' Define the sheet holding the range
    Dim sourceSheet As Worksheet
    Set sourceSheet = ActiveSheet ' This could be a specific sheet like ThisWorkbook.Worksheets("Sheet'sName")
    
    ' Set an initial cell
    Dim initialCell As Range
    Set initialCell = sourceSheet.Range("A3")
    
    ' Set the range with Ctrl + Arrows (there are some alternatives like use the range CurrentRegion see below)
    Dim rangeToScan As Range
    Set rangeToScan = sourceSheet.Range(initialCell.End(xlToRight),initialCell.End(xlDown))
    
    ' This replaces the previous line
    'Set rangeToScan = initialCell.CurrentRegion
    
    ' Loop through cells in first column
    Dim sourceCell As Range
    For Each sourceCell In rangeToScan.Columns(1).Cells
            
        Debug.Print sourceCell.Value
    
    Next sourceCell

End Sub

让我知道它是否有效

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。