I have a date slicer connected to a pivottable and two pivotcharts. I want this slicer to automatically filter to todays date. I recon I have to do this using VBA code, so I
@lars-rotgers Answer over is 100_% correct for cases where there is no pivottable, but for cases where there is a pivottable this is the right solution:
Sub SetTodaysDate()
Dim today As Date
today = Now
Dim todayString As String
todayString = Format$(today, "dd.mm.yyyy")
ThisWorkbook.SlicerCaches("Slicer_Date").ClearManualFilter
ActiveWorkbook.SlicerCaches("Slicer_Date").VisibleSlicerItemsList = Array( _
"[Period].[Date].&[" & todayString & "]")
End Sub
The [Period].[Date]has to correspond to the dimensions etc in your cube.
The name Date Slicer
is probably incorrect. If I record a macro and select a date, then view my code, I get this:
With ActiveWorkbook.SlicerCaches("Slicer_Date")
.SlicerItems("1/1/2015").Selected = False
.SlicerItems("10/5/2015").Selected = False
.SlicerItems("10/26/2015 13:46").Selected = False
End With
So the object name for the slicer is in my macro code Slicer_Date
. Try to record a macro to find the object name for the slicer. Then use that name in your code above.
Edit for comment
I have changed the code a little bit to fit it for my date format:
Sub SlicerSelectToday()
Dim today As Date
today = Now
Dim todayString As String
todayString = Format$(today, "m/d/yyyy") ' I have US date
Dim item As SlicerItem
For Each item In ThisWorkbook.SlicerCaches("Slicer_Date").SlicerItems
If item.Name = todayString Then
item.Selected = True
Else
item.Selected = False
End If
Next item
ThisWorkbook.RefreshAll
End Sub
This sub
is working, and selects the date of today in the slicer.