Macro - Filter Pivot Table multiple does not contain

  • I am trying to write a macro that will filter a pivot table. I want to filter out anything that contains one of two values; "Hosting", and "Infrastructure". I am getting an error on the following macro. It will only filter out the first value. I have tried multipe versions and cannot seem to find a way to filter out anythign containing two different values. Any assistance would be appreciated.


    Sub testFilter()
    Dim PTitle As PivotField
    Set PTitle = ActiveSheet.PivotTables(1).PivotFields("Project Title")
    With PTitle
    .ClearAllFilters
    .PivotFilters.Add xlCaptionContains, , "Hosting"
    .PivotFilters.Add xlCaptionContains, , "Infrastructure"
    End With
    End Sub



    I error out when I get to the second filter. I can get this to work with one filter, but not to filter out two different values.

  • Re: Macro - Filter Pivot Table multiple does not contain


    Also tried this with the same issue of only filtering out the first value and then erroring out.


    Sub Filter()
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Project Title"). _
    PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:= _
    "Hosting"
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Project Title"). _
    PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:= _
    "Infrastructure"
    End Sub

  • Re: Macro - Filter Pivot Table multiple does not contain


    Excel 2010 PivotTable
    Each Type of PivotFilter (xlCaption..., xlValue...)
    can have only one Value1 (no OR condition)
    Alternative methods:
    1. PivotField("x").PivotItems("y").Visible=True/False
    2. Helper Column in source data
    3. PowerPivot with Calculated Column

  • Re: Macro - Filter Pivot Table multiple does not contain


    Herbsds7, Thanks for the input.


    I now have a modified script as follows, but seem to be erroring out on the line

  • Re: Macro - Filter Pivot Table multiple does not contain


    Solved! No need for Power pivot and definitely possible. If anyone else out there would like to have multiple lable filters here you go:


  • Re: Macro - Filter Pivot Table multiple does not contain


    Also speed it up by turning off manual updates


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!