Good day, Excel masters. I'm a first time poster to MrExcel, hopefully not breaking any forum rules. I am trying to use a macro to filter a slicer with only two options: Y or N. I want to filter out all rows with N in a certain column. The slicer is connected to a table. There are no pivot tables or anything else. My code is as follows:
With ActiveWorkbook.SlicerCaches("Slicer_SB")
.SlicerItems("Y").Selected = True
.SlicerItems("N").Selected = False
Debug.Print "Visible Slicer Items: "
End With
This code works fine as long as the table contains both Y and N items. But if the table only contains Y's, then the N item disappears from the slicer, and my macro produces a run time error '5': Invalid procedure or argument and refers me to the line colored in red text above that refers to slicer item "N".
Is there a way to test to see if slicer item N exists so the code won't break?
I have tried many things and researched for hours without any answer so far. I have also tried commenting out the red line above, but then the code doesn't filter to just the Y's, it includes the Y's and N's.
With ActiveWorkbook.SlicerCaches("Slicer_SB")
.SlicerItems("Y").Selected = True
.SlicerItems("N").Selected = False
Debug.Print "Visible Slicer Items: "
End With
This code works fine as long as the table contains both Y and N items. But if the table only contains Y's, then the N item disappears from the slicer, and my macro produces a run time error '5': Invalid procedure or argument and refers me to the line colored in red text above that refers to slicer item "N".
Is there a way to test to see if slicer item N exists so the code won't break?
I have tried many things and researched for hours without any answer so far. I have also tried commenting out the red line above, but then the code doesn't filter to just the Y's, it includes the Y's and N's.