Pergunta

I'd like to clear the filters of all slicers within a sheet but leave the other slicers "un-refreshed".

I have seen the following code that refreshes all slicers within the workbook but can't get it to work solely on a particular sheet only.

Dim slcr As SlicerCache

For Each slcr In ActiveWorkbook.SlicerCaches

    slcr.ClearManualFilter
Next slcr

Any help gratefully received!

Thanks

Foi útil?

Solução

After a bit of playing around and testing (never used slicers through VBA before) I worked out the below.

However, be advised that a slicercache can have slicers on different worksheets. A slicer is linked to a slicer cache which in turn is linked to a data source (e.g. a pivot table). If any slicer is changed, then the slicer cache is changed which changes the pivot table as well. You cannot have 1 slicer with a filter on and another slicer linked to the same pivot table with a filter off, as the pivot table is updated by a slicer's filters. It would't make sense. ;-)

What does make sense is having multiple slicers and multiple pivot tables - using the below will indeed clear the filters for one slicer / cache / pivot but not change any others. This is the only way it could work so hopefully that was what you wanted anyway!!

Sub test()
    RefreshSlicersOnWorksheet ActiveSheet
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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top