Refresh all pivottables when deactive data sheet

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
2,681
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi
I have this deactive macro for Sheet1 (data sheet)(create by right-click on sheet name and insert module):
Code:
Private Sub Worksheet_Deactivate()
    Run "PivotMacro"
End Sub

and this is Pivotmacro :

Code:
Sub PivotMacro()
Dim pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
    Next pt
  Next ws
End Sub

when I run pivotmacro is working and see result changed, but when I change or add data on sheet1 (data sheet) and go to other sheets, I see the results don't change and macro didn't work. And also don't show errors and if run pivotmacro, it is work perfectly?! why?

Please Help
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Your pivot macro code is not actually correct. It should read:

Rich (BB code):
Sub PivotMacro()
Dim pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ws.PivotTables
        pt.RefreshTable
    Next pt
  Next ws
End Sub
 
Upvote 0
Hi RoryA
I apply your modifications but the results are same.
And my sheet name and product name is persian name. Are they effect on macro?

Your pivot macro code is not actually correct. It should read:

Rich (BB code):
Sub PivotMacro()
Dim pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
    For Each pt In ws.PivotTables
        pt.RefreshTable
    Next pt
  Next ws
End Sub
 
Upvote 0
Where did you put the Worksheet_deactivate code?
 
Last edited:
Upvote 0
No you shouldn't insert a module. Right click the sheet tab, choose View code, then paste it into the module that is already there.
 
Upvote 0
No you shouldn't insert a module. Right click the sheet tab, choose View code, then paste it into the module that is already there.

Thanks RoryA. that's it. it works very well.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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