VBA to keep the pivot tables from updating its filters in tandem

CoconutP

New Member
Joined
Aug 11, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi, my VBA below is working fine, something in this code had caused all the pivot tables in each of my worksheet to filter a pivot field in tandem. But I would like each pivot to apply the filter independently. I think it has something to do with the Cache command. Can you please help?

VBA Code:
Sub Filter_Pivot()
 
 Dim WS_Count As Integer
 Dim i As Integer
 Dim x As Integer
 Dim ws As Worksheet
  
' Set WS_Count equal to the number of worksheets in the active workbook

WS_Count = ActiveWorkbook.Worksheets.Count

For i = 1 To WS_Count

Worksheets(i).Activate
       
If Worksheets(i).name <> "Data" Then
         
    With Worksheets(i).PivotTables("PivotTable1")

        .PivotCache.MissingItemsLimit = xlMissingItemsNone

        .PivotCache.Refresh
        
        
        
        With Worksheets(i).PivotTables("PivotTable1").PivotFields("Name").ClearAllFilters
        End With
        

        With Worksheets(i).PivotTables("PivotTable1").PivotFields("Name")
        
                    '---hide all items except item 1
       For x = 1 To .PivotItems.Count
        If x = i Then
        
        .PivotItems(x).Visible = True
        
        Else
        .PivotItems(x).Visible = False
        
        End If
        
        
 
        Next x
               
        
        
 Worksheets(i).Rows("17:23").Hidden = True
 
 

        End With

    End With
 
End If
 
Next i
 
 End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The code above does not look like it should impact multiple pivot tables on the one sheet. There are a number of things that are linked based on Cache with refresh and date grouping being a couple of them, but filter is not one of them.
It is cycling through all the sheets though and updating PivotTable1 on every sheet.

One thing that looks wrong is the below.
Rich (BB code):
                    '---hide all items except item 1
       For x = 1 To .PivotItems.Count
               If x = i Then ' Should this be 1 (one) not "i"
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,734
Members
449,094
Latest member
dsharae57

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