VBA Macro Not Detecting Slicer Correctly in Excel Workbook

bigodudo

New Member
Joined
Jan 16, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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.
It always prints 0 when there is any change in the data slicer. Before, it never printed anything at any time. So we made progress.
However, what I need is for it to print 0 when no filter is selected and 1 when any filter is selected.

I'm a novice in VBA. In the debugging I did with the help of Chat GPT, it indicated that the code was unable to detect the slicers when determining whether there was a filter or not, as if they didn't exist.

I appreciate the effort of anyone who can help in advance. Thank you.

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:

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
' Atualiza a célula B3 na planilha 'DRE TOTAL' com 1 se houver filtros ativos, 0 se não houver
ThisWorkbook.Sheets("DRE TOTAL").Range("B3").Value = IIf(ExistemFiltrosAtivos(), 1, 0)
End Sub

Function ExistemFiltrosAtivos() As Boolean
' Verifica se há filtros ativos em qualquer das Segmentações de Dados especificadas
Dim slicer As slicer
Dim slicerNames As Variant
Dim name As Variant

' Define os nomes das Segmentações de Dados
slicerNames = Array("Descr Un Analítica", "Un resumo", "Un gestor", "Un Operação")

' Itera por cada nome de Segmentação de Dados
For Each name In slicerNames
On Error Resume Next ' Ignora erros
Set slicer = ActiveWorkbook.Slicers(name)
On Error GoTo 0 ' Para de ignorar erros

' Verifica se a Segmentação de Dados existe
If Not slicer Is Nothing Then
' Verifica se há filtros ativos na Segmentação de Dados
If slicer.SlicerCache.VisibleSlicerItems.Count < slicer.SlicerCache.SlicerItems.Count Then
ExistemFiltrosAtivos = True
Exit Function ' Sai do loop se encontrar qualquer filtro ativo
End If
End If
Next name

' Se não houver filtros ativos em nenhuma Segmentação de Dados, retorna Falso
ExistemFiltrosAtivos = False
End Function
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top