excel not refreshing all pivot tables

穿精又带淫゛_ 提交于 2020-01-04 05:39:15

问题


I am trying to refresh pivot tables using vba.
I have code that modifies the underlying table query, and refreshes the query.
Once that is done, it refreshes the pivot tables, which in turn will refresh a summary sheet. For some reason, the final pivot table is not being refreshed. It will refresh normally.

Methods tried:

1.

Dim PC As PivotCache
For Each PC In ThisWorkbook.PivotCaches
    While DateDiff("s", PC.RefreshDate, Now) > 60
    PC.Refresh
    Wend
Next

2.

Sheets("Pivots").PivotTables("Pivot1").PivotCache.Refresh
Sheets("Pivots").PivotTables("Pivot2").PivotCache.Refresh
Sheets("Pivots").PivotTables("Pivot3").PivotCache.Refresh
Sheets("Pivots").PivotTables("Pivot4").PivotCache.Refresh

3.

ThisWorkbook.RefreshAll

I have tried including a DoEvents before refreshing pivot tables, after the refresh of the tables has occurred.
I have also tried copying the file to another file, and recreating it there. the effect stays, it simply moves to whichever pivot table it considers the last one created. File size is 279Kb as xlsb, and 336 Kb as xlsm.
Data for the tables is coming from SQL server 2012, and all tables are on the same excel sheet. The tables are updated, but the pivot is not.


回答1:


Try something like

Sub refreshTables()
Dim pt As PivotTable
Dim ws As Worksheet
For Each ws In Worksheets
    For Each pt In ws.PivotTables
        pt.RefreshTable
    Next pt
Next ws
End Sub


来源:https://stackoverflow.com/questions/34421515/excel-not-refreshing-all-pivot-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!