Sub Test()
Dim dict As Object
Dim lRow As Integer, i As Integer
Dim cel As Range
Dim qString As String
Set dict = CreateObject("Scripting.Dictionary")
lRow = Cells(Rows.Count, 11).End(xlUp).Row
i = 0
For Each cel In Range("K20:K" & lRow)
i = i + 1
dict.Add i, "[Month].[Month Code].&[" & cel.Value & "]"
Next cel
ActiveWorkbook.SlicerCaches("Slicer_Month_Code").VisibleSlicerItemsList = Array(dict.items)
End Sub
Sub Test()
Dim dict As Object
Dim lRow As Integer, i As Integer
Dim cel As Range
Dim qString As String
Set dict = CreateObject("Scripting.Dictionary")
With Worksheets("Sheet2") 'Change Sheet reference as needed
lRow = .Cells(Rows.Count, 1).End(xlUp).Row 'Change Column# as needed
i = 0
For Each cel In .Range("A2:A" & lRow) 'Change Column & Starting cell reference as needed
i = i + 1
dict.Add i, "[Month].[Month Code].&[" & cel.Value & "]"
Next cel
End With
ActiveWorkbook.SlicerCaches("Slicer_Month_Code").VisibleSlicerItemsList = Array(dict.items)
End Sub
Sub Test2()
Dim dict As Object
Dim lRow As Integer
Dim cel As Range
Dim iString As String, aString As String
Dim sLevel As SlicerCacheLevel, sItem As SlicerItem
Set sLevel = ActiveWorkbook.SlicerCaches("Slicer_Month_Code").SlicerCacheLevels("[Month].[Month Code].[Month Code]")
Set dict = CreateObject("Scripting.Dictionary")
For Each sItem In sLevel.SlicerItems
dict.Add sItem.Name, 1
Next
With Worksheets("Sheet2") 'Change Sheet reference as needed
lRow = .Cells(Rows.Count, 1).End(xlUp).Row 'Change Column# as needed
For Each cel In .Range("A2:A" & lRow) 'Change Column & Starting cell reference as needed
iString = "[Month].[Month Code].&[" & cel.Value & "]"
If dict.Exists(iString) Then aString = IIf(aString = "", iString, aString & "," & iString)
Next cel
End With
ActiveWorkbook.SlicerCaches("Slicer_Month_Code").VisibleSlicerItemsList = Array(Split(aString, ","))
Set dict = Nothing
End Sub
In that case you need to have additional reference.
Lets say that you have Cell references in Sheet2 (In column A). And pivot is in Sheet1.
The code is ran while having Sheet1 active. Then the code would change to.
Code:Sub Test() Dim dict As Object Dim lRow As Integer, i As Integer Dim cel As Range Dim qString As String Set dict = CreateObject("Scripting.Dictionary") With Worksheets("Sheet2") 'Change Sheet reference as needed lRow = .Cells(Rows.Count, 1).End(xlUp).Row 'Change Column# as needed i = 0 For Each cel In .Range("A2:A" & lRow) 'Change Column & Starting cell reference as needed i = i + 1 dict.Add i, "[Month].[Month Code].&[" & cel.Value & "]" Next cel End With ActiveWorkbook.SlicerCaches("Slicer_Month_Code").VisibleSlicerItemsList = Array(dict.items) End Sub
Sub except()
Dim dict As Object
Dim lRow As Integer, i As Integer
Dim cel As Range
Dim qString As String
Set dict = CreateObject("Scripting.Dictionary")
Application.CalculateUntilAsyncQueriesDone
With Worksheets("Selections") 'Change Sheet reference as needed
lRow = .Cells(Rows.Count, 35).End(xlUp).Row 'Change Column# as needed
i = 0
For Each cel In .Range("AI2:AI" & lRow) 'Change Column & Starting cell reference as needed
i = i + 1
If (cel <> "(blank)" And cel <> Sheet23.Range("D2")) Then
dict.Add i, "[Category].[MasterBrand].&[" & cel.Value & "]"
End If
Next cel
End With
ActiveWorkbook.SlicerCaches("Slicer_MasterBrand1").VisibleSlicerItemsList = Array(dict.items)
End Sub