Error on VBA Code to Loop Through Slicer Items

Anniemack

New Member
Joined
Oct 11, 2018
Messages
19
After looking through posts I thought I had good code to loop through slicer items to find the one I want, and make it selected, otherwise deselect it. But I keep getting Error 1004 on the "For Each si" line.

VBA Code:
    Dim sc As SlicerCache, si As SlicerItem

    Set sc = ActiveWorkbook.SlicerCaches("Slicer_BU")
  
    sc.ClearAllFilters
    For Each si In sc.SlicerItems
        If si.Name = "ADS" Then
            sc.SlicerItems(i).Selected = True
        Else
            sc.SlicerItems(i).Selected = False
        End If
    Next

What am I doing wrong?

Thank you!
 
Last edited:
Another reason to move away from Arrays was that my next step will be to compare the slicer item not to a string, but to a cell value, so that I can make it more flexible.

Just trying to walk before I run (crawl before walk?).
 
Upvote 0

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.
What happens if you try:

VBA Code:
For Each si In sc.VisibleSlicerItems
 
Upvote 0
can you tell me what the syntax Array([OBUs].[BU].&[ATS]") refers to? the "." between the brackets makes it more than just a structured reference, or at least I can't find what that is.

At this point I'll learn arrays to figure this out.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,533
Members
448,969
Latest member
mirek8991

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