I'm currently facing an issue with a VBA macro in Excel. In my workbook, I have a macro designed to trigger whenever there are changes in Slicer data. The goal is to use the Workbook_SlicerChange function to update a specific cell based on the presence of active filters. However, it seems that the macro isn't being triggered as expected when I interact with Slicer data.
Here's a brief overview of my workbook structure: I have a Slicer in the 'DRE TOTAL' sheet, while the pivot table is in the 'Tabela Fonte' sheet, and the macro is intended to respond to changes in this Slicer.
Here's a snippet of the VBA code I'm using:
vba
Copy code
Private Sub Workbook_SlicerChange(ByVal SlicerCache As SlicerCache)
' Update cell B3 in the 'DRE TOTAL' sheet with 1 if there are active filters, 0 if not
ThisWorkbook.Sheets("DRE TOTAL").Range("B3").Value = IIf(ExistemFiltrosAtivos(), 1, 0)
End Sub
Function ExistemFiltrosAtivos() As Boolean
' Check if there are active filters in all Slicers in the 'DRE TOTAL' sheet
Dim slicer As SlicerCache
Dim wsDRE As Worksheet
' Define the worksheet where Slicers are located
Set wsDRE = ThisWorkbook.Sheets("DRE TOTAL")
' Iterate through all Slicers in the 'DRE TOTAL' sheet
For Each slicer In wsDRE.SlicerCaches
If slicer.VisibleSlicerItems.Count < slicer.SlicerItems.Count Then
ExistemFiltrosAtivos = True
Exit Function ' Exit the loop if any active filter is found
End If
Next slicer
' If there are no active filters in any Slicer, return False
ExistemFiltrosAtivos = False
End Function
I would greatly appreciate any insights or suggestions on why the macro isn't responding to Slicer changes as expected. Thank you!
Bookmarks