AutoFilter通过IF和多个critera

时间:2016-09-29 18:43:15

标签: excel vba excel-vba if-statement

修订问题:以下代码为arrResults添加了过滤器。当宏通过arrResults()过滤字段13时,它仅按捕获的最后一个值进行过滤。当运行调试并查看立即窗口时,我确实看到arrResults()中捕获了多个值,但代码仅按最后一个条目过滤(即如果我单击“已接收/已发送/失败”,则仅返回“失败”。

Option Explicit

Sub Add_Sheet_Update()
Dim LastRow As Long
Dim Rng As Range, str1 As String, str2 As String
Dim i As Long, wsName As String, temp As String
Dim arrResults()

With Sheets("All Call Center Detail")
    LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    Set Rng = .Range("A1:BT" & LastRow)
End With

With Sheets("Search Form")
    str1 = .Range("E9").Text
    str2 = .Range("E13").Text
End With

Dim x As Integer, y As Integer
With Range("R1:S99") ' 2 columns, any # of rows
    For x = 1 To .Rows.Count
        If .Cells(x, 1) Then
            y = y + 1
            ReDim Preserve arrResults(1 To y)
            arrResults(y) = .Cells(x, 2)
            End If
    Next x
End With
Debug.Print Join(arrResults, "/")

Sheets.Add After:=Sheets("Search Form")
ActiveSheet.Name = ("Results")

Sheets("All Call Center Detail").Select
If Not str1 = "" Then Rng.AutoFilter Field:=6, Criteria1:=str1
If Not str2 = "" Then Rng.AutoFilter Field:=7, Criteria1:=str2
If y > 0 Then Rng.AutoFilter Field:=13, Criteria1:=arrResults

Rng.SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Results").Range    ("A1")

Application.CutCopyMode = False
ActiveSheet.ShowAllData

Sheets("Results").Activate
ActiveSheet.Columns.AutoFit
wsName = Format(Date, "mmddyy")
If WorksheetExists(wsName) Then
    temp = Left(wsName, 6)
    i = 1
    wsName = temp & "_" & i
    Do While WorksheetExists(wsName)
        i = i + 1
        wsName = temp & "_" & i
    Loop
End If

ActiveSheet.Name = wsName
Range("A1").Select

End Sub

1 个答案:

答案 0 :(得分:0)

Operator参数添加到AutoFilter语句中:

If y > 0 Then Rng.AutoFilter Field:=13, Criteria1:=arrResults, Operator:=xlFilterValues