Excel VBA - Privot table filter multiple criteria

前端 未结 3 1289
北海茫月
北海茫月 2021-01-12 23:28

I am trying to filter a pivot table with multiple criteria. I\'ve check other posts, but I am getting the error \"AutoFiler method of Range class failed\" when running:

3条回答
  •  花落未央
    2021-01-13 00:24

    You can try the code below:

    Option Explicit
    
    Sub FilterPivotItems()
    
    Dim PT          As PivotTable
    Dim PTItm       As PivotItem
    Dim FiterArr()  As Variant
    
    ' use an array to select the items in the pivot filter you want to keep visible
    FiterArr = Array("101", "105", "107")
    
    ' set the Pivot Table
    Set PT = ActiveSheet.PivotTables("PivotTable3")
    
    ' loop through all Pivot Items in "Value" Pivot field
    For Each PTItm In PT.PivotFields("Value").PivotItems
        If Not IsError(Application.Match(PTItm.Caption, FiterArr, 0)) Then ' check if current item is not in the filter array
            PTItm.Visible = True
        Else
            PTItm.Visible = False
        End If
    Next PTItm
    
    End Sub
    

提交回复
热议问题