Refreshing all the pivot tables in my excel workbook with a macro

前端 未结 10 1332
长发绾君心
长发绾君心 2020-11-29 20:32

I have a workbook with 20 different pivot tables. Is there any easy way to find all the pivot tables and refresh them in VBA?

相关标签:
10条回答
  • 2020-11-29 20:49

    ActiveWorkbook.RefreshAll refreshes everything, not only the pivot tables but also the ODBC queries. I have a couple of VBA queries that refer to Data connections and using this option crashes as the command runs the Data connections without the detail supplied from the VBA

    I recommend the option if you only want the pivots refreshed

    Sub RefreshPivotTables()     
      Dim pivotTable As PivotTable     
      For Each pivotTable In ActiveSheet.PivotTables         
        pivotTable.RefreshTable     
      Next 
    End Sub 
    
    0 讨论(0)
  • 2020-11-29 20:56

    I have use the command listed below in the recent past and it seems to work fine.

    ActiveWorkbook.RefreshAll
    

    Hope that helps.

    0 讨论(0)
  • 2020-11-29 20:57

    Yes.

    ThisWorkbook.RefreshAll
    

    Or, if your Excel version is old enough,

    Dim Sheet as WorkSheet, Pivot as PivotTable
    For Each Sheet in ThisWorkbook.WorkSheets
        For Each Pivot in Sheet.PivotTables
            Pivot.RefreshTable
            Pivot.Update
        Next
    Next
    
    0 讨论(0)
  • 2020-11-29 20:58

    You have a PivotTables collection on a the VB Worksheet object. So, a quick loop like this will work:

    Sub RefreshPivotTables()
        Dim pivotTable As PivotTable
        For Each pivotTable In ActiveSheet.PivotTables
            pivotTable.RefreshTable
        Next
    End Sub
    

    Notes from the trenches:

    1. Remember to unprotect any protected sheets before updating the PivotTable.
    2. Save often.
    3. I'll think of more and update in due course... :)

    Good luck!

    0 讨论(0)
提交回复
热议问题