试图使用第三个标准进行自动过滤,得到编译错误?

时间:2017-10-25 21:44:56

标签: excel vba excel-vba

语法限制真的只有两个吗?所以我必须使用数组或只重复代码块?我的Criteria3:= - .AutoFilter Field中的错误位于Compile error: Named argument not found。我很惊讶它只限于两个。这背后的原因是什么?

Sub CleanData()
    'B b b b 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

1 个答案:

答案 0 :(得分:1)

Criteria3不是有效参数。但要解决此问题,您可以使用数组:

(过滤多个州)

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