Refresh all PivotTables when selecting a Worksheet

0

I need all the pivot tables in my worksheet to be updated every time I select one of my worksheets that serves as a dashboard. I used the most logical code, but I think something is missing, since the macro does not run, since it returns "compile error" for PivotTables :

Private Sub Worksheet_Activate()

    ThisWorkbook.Sheets.PivotTables.RefreshTable

End Sub
    
asked by anonymous 25.03.2017 / 14:38

1 answer

0

Unfortunately, you can not assign any action to all pivot tables on a single line, as if they were a single object. You would have to loop to update them one by one, like this:

Private Sub Worksheet_Activate()

'Dimensionar variáveis
    Dim pt As PivotTable
    Dim ws As Worksheet

'Atualiza Pivot Tables uma a uma
    For Each ws In ActiveWorkbook.Worksheets
        For Each pt In ws.PivotTables
            pt.RefreshTable
            Next pt
        Next ws

End Sub
    
25.03.2017 / 14:43