Filtering Slicer based on cell value

rcarmichael

New Member
Joined
Aug 10, 2012
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Good morning all,
I have already searched on this and other forums for where I'm going wrong, but nothing I've found has helped...
I am trying to filter a year-based slicer from the values in two cells. The code I've got so far works if &[Year1] is changed to &[2022] etc., but trying to pull in the actual cell value from A3 or A6 respectively throws an error.
The code I have so far is:
Sub Sales_Overview()
'
' Sales_Overview Macro
'

'
Dim Year1 As Long
Dim Year2 As Long

Year1 = Worksheets("Sales Overview").Cells(3, "A").Value
Year2 = Worksheets("Sales Overview").Cells(6, "A").Value


ActiveWorkbook.SlicerCaches("Slicer_Calendar").VisibleSlicerItemsList = Array( _
_
"[Date].[Calendar].[Year].&[Year1]", "[Date].[Calendar].[Year].&[Year2]")

End Sub
Any assistance is greatly appreciated,
Sincere regards,
Ryan
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
In addition, it doesn't NEED to be the slicer, it can instead be the OLAP Pivot Table directly:
VBA Code:
    ActiveSheet.PivotTables("Pivot").PivotFields("[Date].[Year].[Year]"). _
        VisibleItemsList = Array("[Date].[Year].&[[COLOR=rgb(226, 80, 65)]2021[/COLOR]]", "[Date].[Year].&[[COLOR=rgb(226, 80, 65)]2022[/COLOR]]")
But the principle is the same... how do I get the Year# Variables in there?
Sincere regards,
Ryan
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
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