VBA to Filter Pivot Table Columns Labels using Filter List

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,513
Office Version
  1. 2024
Platform
  1. Windows
Hi Experts
Reference to my older post - VBA to Filter Column Labels using Filter List

I got help from one of the experts at the forum.

Until recently the below code was working well. Today when I actually needed to use filter I realized the code clears all filter from columns labels but does not filter them back.

I tried hard to understand where things might be going wrong. But Couldn't.

Please help me.
Thanks in advance

VBA Code:
'To Filter D1 Field as per the given Range - But ask before using filter
    Dim x1 As Integer
    x1 = MsgBox("Do you want to Filter Columns?", vbQuestion + vbYesNo + vbDefaultButton1, "")
   
    If x1 = vbYes Then
        Dim PI As PivotItem, FAr() As Variant, AI As Variant, bEx As Boolean, n As Long
       
        Application.ScreenUpdating = False
       
        FAr = Range("PFilters[Name]").Value
       
        With ActiveSheet.PivotTables("ExpAnalysis").PivotFields("D1").ClearAllFilters
            n = 0
            For Each PI In .PivotItems
                bEx = False
            For Each AI In FAr
            If LCase(AI) = LCase(PI.Value) Then
                bEx = True
                Exit For
            End If
            Next
            If bEx = False Then
                n = n + 1
                If n < .PivotItems.Count Then
                  PI.Visible = False
                Else
                  .ClearAllFilters
                End If
            End If
        Next
        End With
   
    Application.ScreenUpdating = True
   
    Else
    End If
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,224,817
Messages
6,181,147
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