Q:

Excel 2013 vba - Refresh Pivot Tables - Loop

I have some vb code that will refresh my pivot tables for me, is there a way to loop this so I can refresh pivot tables 1 to 20 say? Rather than listing 20 lines of code below Eg
ActiveWorkbook.PivotTables(1).PivotCache.Refresh
ActiveWorkbook.PivotTables(2).PivotCache.Refresh
ActiveWorkbook.PivotTables(3).PivotCache.Refresh

Etc..
Many thanks in advance! 

A:

As an example: If you want to refresh all pivot caches in your workbook, you can do something like this:
Sub RefreshPivotCache()
    Dim ws As Worksheet
    Dim PT As PivotTable

    For Each ws In ActiveWorkbook.Worksheets '<~~ Loop all worksheets in workbook
        For Each PT In ws.PivotTables        '<~~ Loop all pivot tables in worksheet
            PT.PivotCache.Refresh
        Next PT
    Next ws
End Sub

