宏自动过滤器无法正常工作

时间:2016-10-24 16:21:02

标签: vba autofilter

以下程序显示错误消息“'AutoFilter'Range类失败的方法”,我无法找到解决方法:

Sub filterforaging()

' filterforaging Macro
'

'
    Columns("i:i").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Rows("1:1").Select
    Selection.AutoFilter
    Range("I1").Select
    ActiveSheet.Range("$A$1:$I$10000").AutoFilter Field:=1, Criteria1:=Array( _
        ">10 days", "5-8days", "8-10days"), Operator:=xlFilterValues
    Application.CutCopyMode = False
End Sub

1 个答案:

答案 0 :(得分:1)

首先关闭">10 days"必须为"*>10 days",以(我猜)避免">"部分导致比较条件

但是,再次出现了与"*>10 days"过滤器相关的奇怪行为:

  • 如果您单独过滤

    .AutoFilter field:=1, Criteria1:="*>10 days"
    

    它会正确过滤所需的行

  • 如果您按其过滤而只过滤其他值

    .AutoFilter field:=1, Criteria1:=Array("*>10 days", "8-10days"), Operator:=xlFilterValues  
    

    它仍能正常工作

  • 但如果您按其过滤另外两个值

    .AutoFilter field:=1, Criteria1:=Array("*>10 days", "5-8days", "8-10days"), Operator:=xlFilterValues  
    

    然后它只会过滤其他两个值

到目前为止,我只能进行以下解决方法:

Dim rng1 As Range, rng2 As Range

With ActiveSheet.Range("$A$1:$I$10000")
    .Rows(1).AutoFilter
    .AutoFilter field:=1, Criteria1:="*>10 days"
    If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) - 1 > 0 Then Set rng1 = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)

    .AutoFilter field:=1, Criteria1:=Array("5-8days", "8-10days"), Operator:=xlFilterValues
    If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) - 1 > 0 Then Set rng2 = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)

    .AutoFilter
    .Columns(1).Resize(.Rows.Count - 1).Offset(1).EntireRow.Hidden = True
    If Not rng1 Is Nothing Then rng1.EntireRow.Hidden = False
    If Not rng2 Is Nothing Then rng2.EntireRow.Hidden = False
End With