Question : Excel VBA code to automatically set a Pivot Table filter on 2 specific values in a field

I'm trying to write some VBA code to have a macro automatically set a pivot table filter to only equal 2 specific values for that field & refresh it.  I couldn't figure this out by looking @ the code produced by the Macro Recorder.  For example, the Pivot Table has a field called "Business Unit".  In this field there's about 50 or so values that I don't need to be included in the table.  So, we currently manually go in & clear the filter for the Business Unit field & then manually scroll down to select the values we need.  We'll say that the only 2 values we need shown in this field are either equal to "Apple" or "Orange" & these 2 values will never change.  Can someone help me with the proper syntax for to filter the pivot table & then refresh it?  Please let me know if I'm being too vague or if you need any more information.  

Thanks as always for your help!

Answer : Excel VBA code to automatically set a Pivot Table filter on 2 specific values in a field

Try this.  You'll need to change my pivot table name "ActivityEstimatesPivotTable" to be the name of your pivot table and you'll need to change the two criteria points in my If statement to what you want.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Sub ApplyPivotFilter()
    Dim pItem As PivotItem
    With ActiveSheet.PivotTables("ActivityEstimatesPivotTable").PivotFields( _
        "Status")
        
        For Each pItem In .PivotItems
        
            If (pItem.Value <> "4 - Resolved") And _
               (pItem.Value <> "Verified") Then
                pItem.Visible = False
            End If
        Next pItem
        
    End With
End Sub
Random Solutions  
 
programming4us programming4us