Macro to update all pivots in a workbook

df9864

Board Regular
Joined
Sep 28, 2004
Messages
108
Hopefully this is a relatively simple - but does anyone have some code which will cycle through all the worksheets of a workbook and refresh any pivot tables.
Not all worksheets will have a pivot and there could be more than 1 pivot table per worksheet (tho this is unlikely and not a deal breaker).

Any help would be much appreciated.
Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:

Code:
Sub Pvt_Refresh()
Dim wb As Workbook, ws As Worksheet
Dim pvt As PivotTable

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets
    For Each pvt In ws.PivotTables
        ws.PivotTables(pvt).PivotCache.Refresh
    Next
Next
End Sub

I haven't had a chance to validate it.
 
Upvote 0
Thanks for the code - looked good to me but I got the following error as it tried to refresh the first pivot (which it found by name)

Run-time error '1004':
Method 'PivotTables' of object_'Worksheet' failed.
 
Upvote 0
Code:
Sub Pvt_Refresh()
Dim wb As Workbook, ws As Worksheet
Dim pvt As PivotTable

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets
    For Each pvt In ws.PivotTables
        ws.PivotTables(pvt.Name).PivotCache.Refresh
    Next
Next
End Sub

Missed off .Name

Try it again
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,219
Members
452,895
Latest member
BILLING GUY

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