VBA to Edit Multiple PivotTables Fields at Once on same worksheet

WWII_Buff

Board Regular
Joined
Nov 13, 2017
Messages
88
Hello all!

I am new to VBA I am trying to update multiple pivot tables on sheet "data", based on drop down list on sheet "Flash" to update graphs on sheet "Flash" as well.
I found the code to update a single pivot table but editing it to do several gives me an error at [FONT=&quot]pt.RefreshTable.
[/FONT]
Thanks!

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
'This line stops the worksheet updating on every change, it only updates when cell
'B4 is touched
If Intersect(Target, Range("B4")) Is Nothing Then Exit Sub
 
'Set the Variables to be used
Dim pt As PivotTable
Dim ws As Worksheet
Dim Field As PivotField
Dim NewCat As String
 
'Here you amend to suit your data
Set ws = Worksheets("data")
    For Each pt In ws.PivotTables
Set Field = pt.PivotFields("Owner")
Next pt
NewCat = Worksheets("Flash").Range("B4").Value
 
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
:warning:[FONT=arial black]pt.RefreshTable[/FONT]:warning:


End With
 
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This works for only "PivotTable3".
How can I add multiple PivotTables to the macro that reside on the same worksheet?


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
'This line stops the worksheet updating on every change, it only updates when cell
'B4 is touched
If Intersect(Target, Range("B4")) Is Nothing Then Exit Sub
 
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
 
'Here you amend to suit your data
Set pt = Worksheets("data").PivotTables("PivotTable3")
Set Field = pt.PivotFields("Owner")
NewCat = Worksheets("Flash").Range("B4").Value
 
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,070
Messages
6,128,610
Members
449,460
Latest member
jgharbawi

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