Wednesday, August 26, 2020

Clear Pivot cache in excel : paste it on thisworkbook and refresh connection

 Private Sub Workbook_Open()

    Dim xPt As PivotTable

    Dim xWs As Worksheet

    Dim xPc As PivotCache

    Application.ScreenUpdating = False

    ThisWorkbook.RefreshAll

    For Each xWs In ActiveWorkbook.Worksheets

        For Each xPt In xWs.PivotTables

            xPt.PivotCache.MissingItemsLimit = xlMissingItemsNone

        Next xPt

    Next xWs

    For Each xPc In ActiveWorkbook.PivotCaches

        On Error Resume Next

        xPc.Refresh

    Next xPc

    Application.ScreenUpdating = True

End Sub