VBA to filter multiple pivot tables across entire workbook.

semper11

New Member
Joined
Dec 12, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Good evening,

Not a VBA expert at all so finally reached a point where I am stuck. I have a data source with inventory information that is already set up in a pivot table. I have a list of 190 reference numbers that each need there own version of the original pivot table and on their own individual sheet. I was able to put together a couple of simple macros that did the sheet creation and naming, but all the filtering had to be done manually. I would love to be able to figure out how to run a macro that would automate the filtering across the workbook based on a specific cell value on each page that I have linked to the original list. I tried a few different codes I found online and found one that would change the filter on a specific sheet/pivot table and when I combined it with another, I was able to get it to populate across multiple sheets, but it applied the same value to the filter across the sheets instead of reading the cell on each sheet. In experimenting, I lost that code and couldn't remember what I did so this is the current code that I have now:

VBA Code:
Sub ChangePivotFilter()
Dim WS As Excel.Worksheet
Dim aWB As Excel.Workbook
Dim myPivot As Excel.PivotTable
Dim myPivotField As Excel.PivotField 
Dim strFilter As String 
Set aWB = ActiveWorkbook 
For Each WS In aWB.Worksheets 
For Each myPivot In WS.PivotTables 
Set myPivotField = Nothing 
On Error Resume Next 
Set myPivotField = myPivot.PivotFields("FACILITY_CODE") 
myPivotField = aWB.ActiveSheet.Range("C1").Value 
Next 
myPivot 
Next 
WS 
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
try.
VBA Code:
Sub ChangePivotFilter()
    Dim WS As Excel.Worksheet
    Dim aWB As Excel.Workbook
    Dim myPivot As Excel.PivotTable
    Dim myPivotField As Excel.PivotField
    Dim strFilter As String
    
    Set aWB = ActiveWorkbook
    strFilter = "What you want to filter"
    For Each WS In aWB.Worksheets
        For Each myPivot In WS.PivotTables
'            Set myPivotField = Nothing
'            On Error Resume Next
            Set myPivotField = myPivot.PivotFields("FACILITY_CODE")
            myPivotField.ClearAllFilters
            myPivotField.PivotFilters.Add2 Type:=xlCaptionEquals, Value1:=strFilter
        Next myPivot
    Next WS
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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