Prevent Excel from refreshing Pivot Table until told to do so by VBA

David Gard picture David Gard · Sep 28, 2012 · Viewed 24.3k times · Source

I have an Excel Template (.xlt) that contains a Worksheet for report data, and 4x Worksheets with Pivot Tables.

When our reporting system creates a report and applys the template to it, some of the fields referenced by the Pivot Tables don't exist, as a Macro is run to create them as part of the process of the reporting system.

However, bescuse these fields do not exist, Excel is throwing up an error (Error occurred while accessing component property/method: REFRESH. Reference is not valid.). To try and prevent this, I have unset the option on all Pivot Tables for Refresh data when opening the file.

I know that I can refresh the Pivot Tables using VBA, but is there a way to prevent the Pivot Tables refreshing themsleves when the documnet is opened, and instead refresh them through VBA, after the new fields have been created? Thanks.

Function UpdatePivots()
    ActiveWorkbook.Sheets("DJG Marketing - Client List by ").PivotTables("PivotTable1").PivotCache.Refresh
    ActiveWorkbook.Sheets("DJG Marketing - Client List by ").PivotTables("PivotTable2").PivotCache.Refresh
    ActiveWorkbook.Sheets("DJG Marketing - Client List by ").PivotTables("PivotTable3").PivotCache.Refresh
    ActiveWorkbook.Sheets("DJG Marketing - Client List by ").PivotTables("PivotTable4").PivotCache.Refresh
End Function

Answer

Alex Gordon picture Alex Gordon · Sep 28, 2012

here's what you need:

' Disable automatic calculation
Application.Calculation = xlCalculationManual
' do regular operation here
' Force a calculation
Application.Calculate
' Then remember to run automatic calculations back on
Application.Calculation = xlCalculationAutomatic

taken from: http://www.zerrtech.com/content/stop-vba-automatic-calculation-using-applicationcalculation-manual