Refreshing Pivot Tables

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a sub like this.
VBA Code:
Sub RefreshPivotTables()
    Sheet1.Activate
    Call RefreshActiveSheetPivotTables
    Sheet2.Activate
    Call RefreshActiveSheetPivotTables
    Sheet3.Activate
    Call RefreshActiveSheetPivotTables
End Sub

Which calls

VBA Code:
Sub RefreshActiveSheetPivotTables()
   Dim PivotTbl As PivotTable
   On Error Resume Next

   For Each PivotTbl In ActiveSheet.PivotTables
      PivotTbl.RefreshTable
   Next PivotTbl
End Sub

On a semi-regular basis, not all of the pivot tables are freshed on the active sheet. I can refresh them manually without any issues, but they are just missed in the loop.
It is not any one particular pivot table that is missed, the missed pivot table changes.
What am I doing incorrectly?
Any guidance is greatly appreciated please.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Jeff can you try replacing ".RefreshTable" with ".PivotCache.Refresh"?
 
Upvote 0
Thanks MCLIFTO8. Much appreciated.

However, I know understand why this issue occured. I cam across this article:


which game me the idea that the querry is the same table as some VBA code at the same time. Which then threw up the 1004 error. So as long as I allow the querry to finish before the vba code kicks in, then all is fine.

Makes sense now :)
 
Upvote 0
I actually found an oldy but a goody solution. Sometimes we can forget that the simplest things in life solve the issue.

The issue was that I had querries and vba accessing tables at the same time, when really what was needed was to have the queries finish first, thhe run the vba code.

VBA Code:
Sub RefreshPivotTables()
    Do While Application.CalculationState <> xlDone
       Refresh all the queries
Loop

so by the tme the VBA get sthis code, all of the queries have been refreshed and no more clashes :)


VBA Code:
    Sheet1.Activate
    Call RefreshActiveSheetPivotTables
    Sheet2.Activate
    Call RefreshActiveSheetPivotTables
    Sheet3.Activate
    Call RefreshActiveSheetPivotTables
End Sub

This solution also fixed another post I had using the With statement. :)
 
Upvote 0
Just FYI, you could add an optional worksheet argument to the refresh routine:

VBA Code:
Sub RefreshActiveSheetPivotTables(Optional ws As Worksheet)
   Dim PivotTbl As PivotTable
   On Error Resume Next
   If ws Is Nothing Then Set ws = ActiveSheet
   For Each PivotTbl In ws.PivotTables
      PivotTbl.RefreshTable
   Next PivotTbl
End Sub

then you can either keep the calling code you have, or skip the activating of each sheet and just pass them as arguments:

VBA Code:
Sub RefreshPivotTables()
    Call RefreshActiveSheetPivotTables(Sheet1)
    Call RefreshActiveSheetPivotTables(Sheet2)
    Call RefreshActiveSheetPivotTables(Sheet3)
End Sub
 
Upvote 0
Hi Rory,

thanks for. I will infact impliment your suggestion because it does make the code much neater.

Just wondering though, would it make any difference to the speed of execution?
 
Upvote 0
It’s generally quicker to avoid activating and selecting things.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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