Looping Through Slicers Based on Name

rilzniak

Active Member
Joined
Jul 20, 2012
Messages
288
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,216,076
Messages
6,128,670
Members
449,463
Latest member
Jojomen56

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