Filter Pivot table based upon list using VBA

jakeman

Active Member
Joined
Apr 29, 2008
Messages
325
Office Version
  1. 365
Platform
  1. Windows
I have a pivot table that I would like to try to filter based upon values from a list on another worksheet, using VBA.

the pivot table is on a worksheet called Sheets("temp_UtilPivot") and the name of the pivot table is temp_UtilData_pivot.

The field in the pivot table is called PCP_Name and the list that I want the PCP_Name field to be filtered off of is on the sheet called Sheets("Lists") and the range of values is stored as a named range = PCP_Selections. PCP_Selections is a dynamic named range that changes in size based upon values added.

I'm using Excel 2010.

thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Andrew - honestly, I've scoured the internet for something and haven't found any code yet...then I tried to put my own piece of code that I put together but my workbook crashed and I lost what I put together before...frustrating.
 
Upvote 0
Does this work for you?

Code:
Sub Test()
    Dim PI As PivotItem
    With Worksheets("temp_UtilPivot").PivotTables("temp_UtilData_pivot").PivotFields("PCP_Name")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("PCP_Selections"), PI.Name) > 0
        Next PI
    End With
End Sub
 
Upvote 0
Mr Poulsom - you are a certified VBA Rockstar! Yes, it worked perfectly. You saved me an unbelievable amount of time with this code.

Thank you very much, sir!
 
Upvote 0
Hi, Andrew, I see your picture on the boards a lot and always know that the answers are good. This is awesome though! I've been trying to figure this out for days now. I've tried complex codes, and stacked For Loops, and Selcting Cases and Ifs. Thank you so much for helping all of us with codes! Happy holidays!

Eric


Does this work for you?

Code:
Sub Test()
    Dim PI As PivotItem
    With Worksheets("temp_UtilPivot").PivotTables("temp_UtilData_pivot").PivotFields("PCP_Name")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("PCP_Selections"), PI.Name) > 0
        Next PI
    End With
End Sub
 
Upvote 0
I know this is an old post but when I try to run the code I am getting an "Object Required" error. any idea why? Thanks!
 
Upvote 0
I know this is an old thread - but I am 6 different pivot tables and am therefore recreating this code 6 times. Everything works, but it takes about a minute for all of the data to populate. Being a complete VBA novice, is there something about the way this is coded (and if I duplicate it several times) that is redundant or taking up too much time?
 
Upvote 0
Speed will depend on the number of Pivot Items in the Pivot Field. Have you set Calculation to manual?

Unfortunately this does not apply to me - I have a dashboard which draws data from the pivot tables.

Here's my code after replicating 6x; is there anything that looks off?



Code:
Sub WTD()    Dim PI As PivotItem
    With Worksheets("Pivot").PivotTables("WTDPlan").PivotFields("FY Week")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("WTDTest"), PI.Name) > 0
        Next PI
        End With
        
    With Worksheets("Pivot").PivotTables("WTDActual").PivotFields("FY Week")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("WTDTest"), PI.Name) > 0
        Next PI
       
        End With


    With Worksheets("Pivot").PivotTables("MTDPlan").PivotFields("FY Week")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("MTDTest"), PI.Name) > 0
        Next PI
        End With
        
    With Worksheets("Pivot").PivotTables("MTDActual").PivotFields("FY Week")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("MTDTest"), PI.Name) > 0
        Next PI
        End With
    
    With Worksheets("Pivot").PivotTables("YTDPlan").PivotFields("FY Week")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("YTDTest"), PI.Name) > 0
        Next PI
        End With
        
    With Worksheets("Pivot").PivotTables("YTDActual").PivotFields("FY Week")
        .ClearAllFilters
        For Each PI In .PivotItems
            PI.Visible = WorksheetFunction.CountIf(Range("YTDTest"), PI.Name) > 0
        Next PI
        End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top