Is there a quick way to deselect all slicer items in VBA then select only 1?

codeliftsleep

Board Regular
Joined
Apr 14, 2017
Messages
103
I have a slicer that has quite a few values in it and having to loop through all of the items takes quite a while. When a user opens the workbook, it is setting the slicers to show only their data. However, this looping through all the users of each slicer is taking much longer than it needs to.

Basically all I really need to do is to Deselect all of the items(aka, the equivalent of clicking (blank) in VBA code) and then select one item. However it does not appear this is possible, as when I attempt to select blank in VBA code, it doesn't work. Even recording a macro with me clicking blank basically selects blank and then deselects all other items manually, which is pretty much the same as me looping over them and deselecting them, which is what I want to avoid.

Is there no way to accomplish what seems like it should be a very basic task?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this method
- amend the red values to match your requirements

Code:
Sub SlicerTest()
    Const SlicerName = "[COLOR=#ff0000]Region[/COLOR]"
    Dim sc As SlicerCache, sl As Slicer, i As Integer, SlicerValue As String
    
    SlicerValue = GetSlicerValue(Application.UserName)
    For Each sc In ActiveWorkbook.SlicerCaches
        For Each sl In sc.Slicers
            If sl.Name = SlicerName Then
                For i = 1 To sc.SlicerItems.Count
                    With sc.SlicerItems(i)
                        .Selected = False
                        If .Value = SlicerValue Then .Selected = True
                    End With
                Next i
                Exit Sub
            End If
        Next sl
    Next sc

End Sub

Private Function GetSlicerValue(user As String)
    Select Case user
        Case "[COLOR=#ff0000]Walter[/COLOR]":  GetSlicerValue = "[COLOR=#ff0000]Reg4[/COLOR]"
        Case "[COLOR=#ff0000]Yongle[/COLOR]":  GetSlicerValue = "[COLOR=#ff0000]Reg3[/COLOR]"
        Case "[COLOR=#ff0000]Pete[/COLOR]r":   GetSlicerValue = "[COLOR=#ff0000]Reg2[/COLOR]"
        Case "[COLOR=#ff0000]Jenny[/COLOR]":   GetSlicerValue = [COLOR=#ff0000]"Reg1[/COLOR]"
        Case Else:      End
    End Select
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top