Filter Excel pivot table using VBA

后端 未结 6 2196
温柔的废话
温柔的废话 2020-12-03 17:40

I have tried copying and pasting solutions from the internet forever now to try to filter a pivot table in Excel using VBA. The code below doesn\'t work.

Sub         


        
6条回答
  •  独厮守ぢ
    2020-12-03 18:36

    You could check this if you like. :)

    Use this code if SavedFamilyCode is in the Report Filter:

     Sub FilterPivotTable()
       Application.ScreenUpdating = False
       ActiveSheet.PivotTables("PivotTable2").ManualUpdate = True
    
       ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode").ClearAllFilters
    
       ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode").CurrentPage = _
          "K123223"
    
      ActiveSheet.PivotTables("PivotTable2").ManualUpdate = False
      Application.ScreenUpdating = True
      End Sub
    

    But if the SavedFamilyCode is in the Column or Row Labels use this code:

     Sub FilterPivotTable()
         Application.ScreenUpdating = False
         ActiveSheet.PivotTables("PivotTable2").ManualUpdate = True
    
          ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode").ClearAllFilters
    
          ActiveSheet.PivotTables("PivotTable2").PivotFields("SavedFamilyCode").PivotFilters. _
        Add Type:=xlCaptionEquals, Value1:="K123223"
    
      ActiveSheet.PivotTables("PivotTable2").ManualUpdate = False
      Application.ScreenUpdating = True
      End Sub
    

    Hope this helps you.

提交回复
热议问题