Set excel slicer to todays date

前端 未结 2 509
小蘑菇
小蘑菇 2020-12-22 10:18

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

2条回答
  •  天命终不由人
    2020-12-22 10:46

    @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.

提交回复
热议问题