自动过滤通过IF和多个标准

修正的问题:下面的代码将filter添加到arrResults。 当macros通过arrResults()过滤字段13时,它只会根据捕获的最后一个值进行过滤。 当运行debugging和查看立即窗口时,我看到在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 

Operator参数添加到您的AutoFilter语句中:

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