Iterate over slicer via VBA and select a single item each time

Since the question I linked you too in my comment does not have an accepted answer (the user never choose to answer or reply to my suggestion), I will provide the solution to your problem here as well.

Sub slicers(slName As String)

    Dim slItem As SlicerItem, slDummy As SlicerItem
    Dim slBox As SlicerCache

    Set slBox = ActiveWorkbook.SlicerCaches(slName)

    'loop through each slicer item
    For Each slItem In slBox.SlicerItems

        'show all items to start
        slBox.ShowAllItems 'or .ClearManualFilter

        'test each item against itself
        For Each slDummy In slBox.SlicerItems

            'if the item equals the item in the first loop, then select it
            'otherwise don't show it (thus showing 1 at a time between the nested loops)
            If slItem.Name = slDummy.Name Then slDummy.Selected = True Else: slDummy.Selected = False

            'more code to process the data (I suspect)

        Next slDummy

    Next slItem

End Sub

Leave a Comment