I wrote some code below to have my pivot table's filters (in the slicer) update when a data validation dropdown is changed. So far, this is what I have but it is not working:
Option Explicit
' sheet module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sc As SlicerCache, si As SlicerItem
If Target.Address = "$F$2" Then ' cell to monitor
Set sc = ActiveWorkbook.SlicerCaches("Slicer_Concat") ' desired slicer
sc.ClearAllFilters
For Each si In sc.SlicerItems
If si.Caption = CStr(Target) Then
si.Selected = True
Else
si.Selected = False
End If
Next
End If
End Sub
I'm not the best at VBA, so I've read through some old threads on this site and a few mrexcel forums that detail this problem. They all say to use the above code, with the only things I've changed being adding in the Option Explicit
command and modifying the name of the slicer and the exact target cell.
I first tried using this code in a new module on the sheet, but nothing happened when I updated the contents of cell F2. Then I tried to move the code to the worksheet itself. This at least generated a reaction from excel when I changed the contents of cell F2, but the response was the following error message:
RUN TIME ERROR '1004': Application-defined or object-defined error
When I go to debug, the error comes up at the following line in my code:
For Each si In sc.SlicerItems
Does anyone have any ideas where to go from here? The slicer doesn't seem affected by anything I've tried. Not even sure if the code should go in a module or the worksheet itself.