如何在宏中匹配2个条件

时间:2018-06-19 16:23:00

标签: excel vba excel-vba

我目前有以下代码用于查找哥伦布的专栏。但是,如何指定我只想通过参考第4行(州)来查找俄亥俄州哥伦布的专栏呢?

Amount = WorksheetFunction.Match("Columbus", Rows("5:5"), 0)

2 个答案:

答案 0 :(得分:0)

尝试遍历所有记录-

Dim Amount As Variant
Dim lngRow as long
lngRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lngRow 'Considering row 1 has headers
    If ActiveSheet.Cells(i, 5) = "Columbus" And ActiveSheet.Cells(i, 4) = "Ohio" Then
        Amount = i
        Exit For
    End If
Next i

谢谢

答案 1 :(得分:0)

使用变量数组并循环遍历,这样会更快:

With Worksheets("Sheet1") 'Change to your sheet
    Dim rngArr() As Variant
    rngArr = .Range(.Cells(4, 1), .Cells(5, .Columns.Count).End(xlToLeft)).Value
    Dim i As Long
    For i = 1 To UBound(rngArr, 2)

        If rngArr(1, i) = "Ohio" And rngArr(2, i) = "Columbus" Then Exit For
    Next i

    If i <= UBound(rngArr, 2) Then
        Dim Amount As Long
        Amount = i
    Else
        MsgBox "Not Found"
    End If
End With