防止Excel刷新数据透视表,直到被VBA告知

我有一个Excel模板(.xlt)包含报表数据的工作表和4x工作表与数据透视表。

当我们的报告系统创build报告并将模板应用于其中时,数据透视表所引用的某些字段不存在,因为运行macros时会将它们创build为报告系统的一部分。

但是,这些字段不存在,Excel正在抛出一个错误(访问组件属性/方法时发生错误:REFRESH。引用无效)。 要尝试和防止这种情况,我Refresh data when opening the file已取消select所有数据透视表Refresh data when opening the file

我知道我可以使用VBA刷新数据透视表,但有没有办法来防止数据透视表在文档打开时刷新微软,而是在创build新字段之后通过VBA刷新它们? 谢谢。

 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 

这里是你需要的:

 ' 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 

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

我非常同意以前的答案。 以下代码(或类似)在很多Excel VBA程序中:

 Sub MyExample() with Excel.Application .ScreenUpdating =False .EnableEvents =False .Calculation = Excel.xlCalculationManual end with on error goto ErrorFoundResetApplication: '>>>>>>>>>>>> 'code in here call UpdatePivots '<<<<<<<<<<< ErrorFoundResetApplication: with Excel.Application .ScreenUpdating = true .EnableEvents =true .Calculation = Excel.xlCalculationAutomatic end with end sub Function UpdatePivots() ActiveWorkbook.RefreshAll '<<<refresh them all in one go End Function