Link to home
Start Free TrialLog in
Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America

asked on

VBA code to automatically refresh Pivot tables and slicers at the worksheet level

I have found various versions of code to refresh Pivot tables, Update Data Models and refresh slicers.  However, I am having an issue with some of the code for clearing the Slicers to reset them back to display all options.

Debug error on "sc.ClearManualFilter"

Can someone point me to the best option for resetting the Pivot tables, slicers and refreshing the underlying Data Model from the Power Query/Pivot?

K
Sub Reset()
Dim pt As PivotTable
    
 Application.ScreenUpdating = False
 RefreshSlicersOnWorksheet ActiveSheet
    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
    Next pt
     If MsgBox("Do you want to refresh the PowerPivot Model?", vbYesNo) = vbYes Then
        ActiveWorkbook.Model.Refresh
    End If

     Application.ScreenUpdating = True
End Sub

Public Sub RefreshSlicersOnWorksheet(ws As Worksheet)
    Dim sc As SlicerCache
    Dim scs As SlicerCaches
    Dim slice As Slicer

    Set scs = ws.Parent.SlicerCaches

    If Not scs Is Nothing Then
        For Each sc In scs
            For Each slice In sc.Slicers
                If slice.Shape.Parent Is ws Then
                    sc.ClearManualFilter
                    Exit For 'unnecessary to check the other slicers of the slicer cache
                End If
            Next slice
        Next sc
    End If

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Karen Schaefer

ASKER

thanks, it seems to be working now,
thanks for the assist
thanks
Pleased that you got it working