I would like to put my two cents. The set of visible slicer elements can be reduced by both independent actions:
- User Selecting Elements in Slicer A. Use the
.Selected method to capture these elements. - Selecting elements in slicer B, which as a result shortens the list of slicer A. To capture these elements, use the
.HasData method.
Please note that you can only see two Slicer_Products elements (apples, bananas), because some other Slicer_Product_Type have an active filter for fruits . The sI.Selected method would still return the entire list of products apples, bananas, carrots ...
If you want both restrictions to be set, intersect both sets. I changed the code of TobiasKnudsen (great answer!) So that it returns a list of elements compressed by both of the above limitations. If sI.Selected = True And sI.HasData = True Then this is the key line in this code.
Option Explicit Sub TestExample() Dim MyArr() As Variant MyArr = ArrayListOfSelectedAndVisibleSlicerItems("Slicer_A") 'now variable MyArr keeps all items in an array End Sub Public Function ArrayListOfSelectedAndVisibleSlicerItems(MySlicerName As String) As Variant 'This function returns an array of the limited set of items in Slicer A 'Limitation is due to both: '(1) direct selection of items by user in slicer A '(2) selection of items in slicer B which in consequence limits the number of items in slicer A Dim ShortList() As Variant Dim i As Integer: i = 0 'for iterate Dim sC As SlicerCache Dim sI As SlicerItem 'for iterate Set sC = ThisWorkbook.Application.ActiveWorkbook.SlicerCaches(MySlicerName) For Each sI In sC.SlicerItems If sI.Selected = True And sI.HasData = True Then 'Here is the condition!!! 'Debug.Print sI.Name ReDim Preserve ShortList(i) ShortList(i) = sI.Value i = i + 1 End If Next sI ArrayListOfSelectedAndVisibleSlicerItems = ShortList End Function
source share