VBA Excel: Refresh pivot tables & tables automatically

Hello experts, I have the following procedure which allows me to refresh the various pivot tables I would like to add some enhancements:

Sub Refresh_Pivot()
Dim PT As pivotTable
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
For Each PT In WS.PivotTables
Next PT
Next WS
End Sub

Open in new window

1.      Run the procedure after I change a data or after a save
2.      Include in the procedure the refresh of the various tables in the workbook

Thank you very much for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Place the following codes on ThisWorkbook Module.
The first code "Workbook_SheetChange" will trigger the ThisWorkbook.RefreshAll if you change the data on any sheet in the workbook.
And the second code will trigger the ThisWorkbook.RefreshAll after you save the workbook.

ThisWorkbook.RefreshAll will refresh all data ranges and available pivot table reports in this workbook.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error GoTo Skip:
Application.EnableEvents = False
Application.EnableEvents = True
End Sub

Private Sub Workbook_AfterSave(ByVal Success As Boolean)
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you very much for your feedback.
Should I add those procedures inside modules, Sheets or ThisModules?
LD16Author Commented:
Ok, I added in ThisWorkbook and it works perfectly. Thank you again for your help!
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome! Glad it worked as desired.
Thanks for the feedback.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.