Search code examples
excelpivot-tableslicers

How to find Dimensions in cube for Excel slicer


I am trying to figure out how to get the dimensions of the cube for my slicer. I have tried next to everything to get it to work, but with no success.

The code below is what I am using to have the slicer set the day to today's date only.

Dim today As Date
today = Now
Dim todayString As String
todayString = Format$(today, "mm/dd/yyyy")
ThisWorkbook.SlicerCaches("Slicer_Created_on").ClearManualFilter
ActiveWorkbook.SlicerCaches("Slicer_Created_on").VisibleSlicerItemsList=
Array( _  
"[Period].[Date].&[" & todayString & "]") ' This is where the error occurs.

The above code was found on this site at the following link: Set excel slicer to todays date

I tried to modify it so that the

"[Period].[Date].&[" & todayString & "]")

was in this format: "[Period]/[Date]/&[" & todayString & "]")

Created two more dimensions to be: Year and month and modified it like this: dim month month=format$(today,"mm") todaystring =format$(today,"dd") Year=format$(today,"yyyy")

"[Month]/&["& todaystring & "]" & [Year]")

Thank you for your time.


Solution

  • After hours of tinkering around, I was not able to figure out how to determine what the cube values are so I used a different code found on this site.

    At this link: Set excel slicer to todays date

    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

    EDIT: If possible avoid looping through all of the date values because it takes longer. I would highly recommend recording a macro of the tasks that you wish to complete with the Pivot Tables then edit the macro for your needs. For example record a macro of your actions while you apply a date filter "Date Filter>Yesterday" value to only show yesterday's data. Something similar to what I did in this link Excel 2013 VBA Pivot Table Select Only Top 5 Items With Ties. I hope this helps someone.