VBA Select slicer item based based on a cell ref, but if the item is not in the slicer then select a specific slicer item

Boytjie79

New Member
Joined
Oct 13, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I am using the below VBA to select a slicer item based on a cell ref. This works fine, however if the item in the cell is not in the slicer then i want it to select an item in the slicer called "No Data"

The code i'm currently using is below, could someone please help me with auto selecting the item "No Data" if the cell ref is not an option in the slicer?

Many Thanks

Sub Temp1()

' Uber Eats Slicer
Dim UEselected As String
UEselected = ActiveSheet.Range("B5").Value

With ActiveWorkbook.SlicerCaches("Slicer_Brand1")
.ClearManualFilter
For Each oSlicerItem In .SlicerItems
If oSlicerItem.Name = UEselected Then
oSlicerItem.Selected = True
Else
oSlicerItem.Selected = False
End If
Next oSlicerItem
End With

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try the following...

VBA Code:
Sub Temp1()

    ' Uber Eats Slicer
    Dim UEselected As String
    UEselected = ActiveSheet.Range("B5").Value
    
    With ActiveWorkbook.SlicerCaches("Slicer_Brand1")
        .ClearManualFilter
        On Error Resume Next
        Dim TargetSlicerItem As SlicerItem
        Set TargetSlicerItem = .SlicerItems(UEselected)
        If TargetSlicerItem Is Nothing Then
            Set TargetSlicerItem = .SlicerItems("No Data")
        End If
        On Error GoTo 0
        If Not TargetSlicerItem Is Nothing Then
            Dim oSlicerItem As SlicerItem
            For Each oSlicerItem In .SlicerItems
                If oSlicerItem.Name = TargetSlicerItem.Name Then
                    oSlicerItem.Selected = True
                Else
                    oSlicerItem.Selected = False
                End If
            Next oSlicerItem
        End If
    End With

End Sub

Hope this hleps!
 
Upvote 0
Try the following...

VBA Code:
Sub Temp1()

    ' Uber Eats Slicer
    Dim UEselected As String
    UEselected = ActiveSheet.Range("B5").Value
   
    With ActiveWorkbook.SlicerCaches("Slicer_Brand1")
        .ClearManualFilter
        On Error Resume Next
        Dim TargetSlicerItem As SlicerItem
        Set TargetSlicerItem = .SlicerItems(UEselected)
        If TargetSlicerItem Is Nothing Then
            Set TargetSlicerItem = .SlicerItems("No Data")
        End If
        On Error GoTo 0
        If Not TargetSlicerItem Is Nothing Then
            Dim oSlicerItem As SlicerItem
            For Each oSlicerItem In .SlicerItems
                If oSlicerItem.Name = TargetSlicerItem.Name Then
                    oSlicerItem.Selected = True
                Else
                    oSlicerItem.Selected = False
                End If
            Next oSlicerItem
        End If
    End With

End Sub

Hope this hleps!
Amazing, thank you so much, this has worked perfectly and has added to my VBA knowledge. Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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