1

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.

BigBen
  • 46,229
  • 7
  • 24
  • 40
Matt
  • 11
  • 1
  • To answer your last question, yes this belongs in the worksheet. – BigBen Jun 14 '23 at 19:11
  • Maybe look at https://stackoverflow.com/a/31828030/478884 – Tim Williams Jun 14 '23 at 22:10
  • Hi Tim, I tried to implement the feedback from that post and it didn't work, and I'm not sure I understand the stagnant reference here in the solution: If sI.Name = "Introduction To Slicer" – Matt Jun 21 '23 at 18:07

0 Answers0