Avatar of Karen Schaefer
Karen Schaefer
Flag 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

VBAMicrosoft Excel

Avatar of undefined
Last Comment
Roy Cox

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Roy Cox

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Karen Schaefer

ASKER
thanks, it seems to be working now,
Karen Schaefer

ASKER
thanks for the assist
Karen Schaefer

ASKER
thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Roy Cox

Pleased that you got it working