Slicer Value/Item selection through Excel VBA

Viewed 30

I would like to know the VBA code to select the one item at a time in the slicer. Actually I have a slicer with 10 items in it. I also recorded the macro to see the code. In that code, it makes true the item that I want to see and all others as false. I want to know if there is any other method where I do not have to make 1 true and all others false.

1 Answers

This macro will set to true only the item that you specify in ItemToFilter.

Sub Slicer_Filtering()
    Dim SlicerName As String
    Dim ItemToFilter As String
    Dim sl As SlicerItem
    
    SlicerName = "Slicer_test" 'Change as needed
    ItemToFilter = "Test3" 'Change as needed
    ThisWorkbook.SlicerCaches(SlicerName).ClearAllFilters
    
    For Each sl In ThisWorkbook.SlicerCaches(SlicerName).SlicerItems
        If sl.Name = ItemToFilter Then
            sl.Selected = True
        Else
            sl.Selected = False
        End If
    Next

End Sub
Related