Select max date from Slicer

mssbass

Active Member
Joined
Nov 14, 2002
Messages
253
Platform
  1. Windows
I'm getting an Application-defined or object-defined error on SI.Selected = False. Can anyone help? Everything I've tried based on my research has failed.

VBA Code:
[
Sub TestSlicer()

Dim sC As SlicerCache
Dim sI As SlicerItem
Dim index As Integer

Set sC = ActiveWorkbook.SlicerCaches("Slicer_ReportDt1")

sC.ClearManualFilter

For index = 1 To sC.SlicerCacheLevels.Count

    For Each sI In sC.SlicerCacheLevels(index).SlicerItems

        If sI.Name = Format$(Date - 1, "yyyy-mm-dd") Then
            sI.Selected = True
        Else
            sI.Selected = False
        End If

    Next sI

Next index

End Sub
]
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Fixed - since slicer was set to data model, it was tougher - but found the fix:

VBA Code:
[
Sub Demo()
Dim slc As SlicerCache
Dim sli As SlicerItem
Dim iDic As Object
Set slc = ThisWorkbook.SlicerCaches("Slicer_ReportDt1")
Set iDic = CreateObject("Scripting.Dictionary")

For Each sli In slc.SlicerCacheLevels(1).SlicerItems
    If sli.Value = Format(Date - 1, "yyyy-mm-dd") Then
        iDic(sli.Name) = 1
    End If
Next

If iDic.Count = 0 Then
    MsgBox "No item selected"
Else
    slc.VisibleSlicerItemsList = iDic.Keys
End If
End Sub
]
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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