Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA – Select the first 2 values of a slicer

I would like to select the two first values of a slicer using VBA.

For now, my code is as follow:

ActiveSheet.SlicerCaches("EXAMPLE").ClearAllFilters
cnt = .SlicerItems.Count
If cnt > 2 Then
    For i = 3 To cnt
        .SlicerItems(i).Selected = False
    Next
End If
End With

Could you please help me on that ?
Thank you

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming the Slicer is in the ActiveWorkbook (replace ActiveWorkbook with a reference to the specific Workbook otherwise) and the Slicer is named "EXAMPLE", then use:

Dim i As Long
With ActiveWorkbook.SlicerCaches("EXAMPLE")
    .ClearAllFilters
    For i = 1 To .SlicerItems.Count
        .SlicerItems(i).Selected = i <= 2
    Next i
End With
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading