尝试使用自动filter的第三个标准,得到编译错误?

语法限制真的只有两个? 所以我必须要么使用数组,要么只是重复代码块? 错误是在Criteria3:=在我的.AutoFilter FieldCompile error: Named argument not found 。 我只是感到惊讶,它只限于两个。 这背后的原因是什么?

 Sub CleanData() 'B bbb boilerplate! Dim sht As Worksheet, lastrow As Long, myrange As Range 'Set references up-front Set sht = ThisWorkbook.Worksheets("MySheet") 'Identify the last row and use that info to set up the Range With sht lastrow = .Cells(sht.Rows.Count, "A").End(xlUp).Row Set myrange = .Range("A2:AS" & lastrow) End With Application.DisplayAlerts = False With myrange 'Apply the Autofilter method to the first column of .AutoFilter Field:=26, _ Criteria1:="Operator Error", _ Operator:=xlOr, _ Criteria2:="Duplicate", _ Operator:=xlOr, _ Criteria3:="Training/Test" 'ERROR HERE 'Delete the visible rows while keeping the header On Error Resume Next .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Rows.Delete On Error GoTo 0 End With Application.DisplayAlerts = True 'Turn off the AutoFilter With sht .AutoFilterMode = False If .FilterMode = True Then .ShowAllData End If End With End Sub 

Criteria3不是一个有效的参数,但为了解决这个问题,你可以使用一个数组:

(过滤多个状态)

 Criteria1:=Array("IL", "IN", "MI", "OH", "WV"), Operator:=xlFilterValues