使用VBA筛选多种组合

时间:2019-02-04 19:34:52

标签: excel vba

我正在尝试在同一行中执行2个不同的过滤器。正如您在下面看到的那样,我正在一个字段中寻找3个数字中的1个,在另一个字段中寻找2个数字中的1个。我不认为这写得正确,因为它仍然为第二组提取比我指示的变量更多的数据。我怀疑它只是在我的“与”或“与”周围,但需要一些帮助。

For i = 2 To rngData.Rows.Count
        ' Check cell of column 10 of row i and copy if matched
        If rngData.Cells(i, 10).Value = "303500" Or rngData.Cells(i, 10).Value = "604066" Or rngData.Cells(i, 10).Value = "613991" And rngData.Cells(i, 3).Value = "ACR/EM-1A" Or rngData.Cells(i, 3).Value = "ACR/EM-1D" Then
            ' Copy over to wsDalreq from row j
            j = j + 1
            rngData.Rows(i).Copy Destination:=wsTempinterior.Cells(j, 1)
        End If
    Next

1 个答案:

答案 0 :(得分:2)

您需要使用括号将两组Or版测试分开。还可以考虑将变量用于性能/维护

编辑:将单元格检查分为两个单独的If

For i = 2 To rngData.Rows.Count
    ' Check cell of column 10 of row i and copy if matched
    val1 = rngData.Cells(i, 10).Value
    val2 = rngData.Cells(i, 3).Value

    If val1 = "303500" Or val1 = "604066" Or val1 = "613991" Then

       If val2 = "ACR/EM-1A" Or val2 = "ACR/EM-1D" Then

            ' Copy over to wsDalreq from row j
            j = j + 1
            rngData.Rows(i).Copy Destination:=wsTempinterior.Cells(j, 1)

        End If

    End If

Next i