Filter multiple Pivots on same worksheet based on cell in that worksheet

manookin

New Member
Joined
Jan 8, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am sure this is a simple fix but I am hoping you can help.

I do not know VBA, but am trying my hand at altering some code i found online. (see How to filter Pivot table based on a specific cell value in Excel? (extendoffice.com).

I was able to modify it to get it to work for 1 Pivot table (See code below) but I am trying to create a dashboard that will have several pivot tables/pivot charts. So I want the code below to update all the pivot tables/charts I put on that workhseet, based on a value in cell B2 of the Dashboard sheet. I am unable to figure out how to get the code to look at more than 1 pivot table. Can anyone help?



Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String

On Error Resume Next
If Intersect(Target, Range("B1:B1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False

For Each
Set xPTable = Worksheets("Dashboard").PivotTables("PivotTable9")
Set xPFile = xPTable.PivotFields("Activity")

xStr = Worksheets("Dashboard").Cells(1, 2)
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

End Sub
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

manookin

New Member
Joined
Jan 8, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
the "For each" should not be there. My apologies to whomever reads the code i was playing with something and forgot to take it out. Code should be as follows

VBA Code:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim xPTable As PivotTable
Dim xPFile As PivotField
Dim xStr As String

On Error Resume Next
If Intersect(Target, Range("B1:B1")) Is Nothing Then Exit Sub
Application.ScreenUpdating = False

Set xPTable = Worksheets("Dashboard").PivotTables("PivotTable9")
Set xPFile = xPTable.PivotFields("Activity")

xStr = Worksheets("Dashboard").Cells(1, 2)
xPFile.ClearAllFilters
xPFile.CurrentPage = xStr
Application.ScreenUpdating = True

End Sub
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
630
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
For Each xPTable In Activesheet.PivotTables
'your code
next
 

Watch MrExcel Video

Forum statistics

Threads
1,122,455
Messages
5,596,231
Members
414,047
Latest member
debbos

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
Top