Looping Through Slicers Based on Name

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
291
Hey, I'm hoping someone has a simple solution that I'm just missing. I'm looking to select slicers within a workbook based on the month and the year - there are other slicers I want to ignore. I'm naming each of the slicers based on the worksheet and am trying to loop through each sheet and select the month and year for each if a slicer exists on that page.

Rich (BB code):
sub SelectSlicers()

dim today as date
dim MonthString as string

today = now
MonthString = format(today,"mmm")

            For Each item In ThisWorkbook.SlicerCaches(ActiveSheet & "_Month_Name").SlicerItems
                If item.Name = MonthString Then
                    item.Selected = True
                Else
                    item.Selected = False
                End If
            Next item

End sub

I've tried a few different ways to replace "ActiveSheet" but none of them have worked. I'm probably missing something simple. Any suggestions?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hey, I'm hoping someone has a simple solution that I'm just missing. I'm looking to select slicers within a workbook based on the month and the year - there are other slicers I want to ignore. I'm naming each of the slicers based on the worksheet and am trying to loop through each sheet and select the month and year for each if a slicer exists on that page.

Rich (BB code):
sub SelectSlicers()

dim today as date
dim MonthString as string

today = now
MonthString = format(today,"mmm")

            For Each item In ThisWorkbook.SlicerCaches(ActiveSheet & "_Month_Name").SlicerItems
                If item.Name = MonthString Then
                    item.Selected = True
                Else
                    item.Selected = False
                End If
            Next item

End sub

I've tried a few different ways to replace "ActiveSheet" but none of them have worked. I'm probably missing something simple. Any suggestions?
I was going about it the wrong way. Here's the solution that worked for me:

Rich (BB code):
            MonthSlicer = ws & "_Month_Name"
           
            For Each item In ThisWorkbook.SlicerCaches(MonthSlicer).SlicerItems
                If item.Name = MonthString Then
                    item.Selected = True
                Else
                    item.Selected = False
                End If
            Next item
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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