VBA code to clear all filters in pivot table fields

BigC

Well-known Member
Joined
Aug 4, 2002
Messages
851
I am having trouble getting the right VBA code to clear all filters from all fields in a pivot table in Excel 2003. Below is what I have tried, but each attempt returns the following error: "Run-time 1004 error. Unable to set the Visible property of the PivotItem class."

Dim pt As PivotTable
Dim pf As PivotField
Set pt = Sheet1.PivotTables(1)

'Attempt 1
For Each pf In pt.PivotFields
pf.ShowAllItems = True
Next pf

'Attempt 2
For Each pf In pt.RowFields
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
Next pf
Even running the following exact code recorded by the macro recorder returns the same error:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Service Type")
.PivotItems("Account").Visible = True
' replicated for each PivotItem
End With
Setting the Visible property to FALSE works, but not when setting to TRUE.

I have searched the web, and seen other users raise the same issue, but have not seen a solution.

Is this a bug in 2003?
Is there a workaround?

Thanks in advance
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

Can you try this code and see if it works for you?

Code:
Sub UnhideAllPivotItems()

Dim p As PivotTable
Dim pf As PivotField
Dim pfi As PivotItem


Set p = Sheet1.PivotTables(1)

'Unhide all non page-field pivot items
For Each pf In p.PivotFields

    For Each pfi In pf.PivotItems

        If Not pfi.Visible Then pfi.Visible = True
    
    Next pfi

Next pf

'Unhide page field pivot items
For Each pf In p.PageFields

    pf.CurrentPage = "(All)"
    
Next pf

End Sub

HTH
DK
 
Upvote 0
Hi Dk

Unfortunately not - it still errors at the "pfi.Visible = True" statement.:(

As pfi.Visible = False works, I also tried making each item hidden and then making it visible to see if that would slip through the trap, but to no avail.

Thanks for the idea.
 
Upvote 0
Hmm, strange. I tested on a new pivot table in 2003 and it worked. Can you try and create a new pivot table for test purposes, hide some items and see if the code I posted works on the new test pivot table? At least that will let us determine if there is something specific about your original pivot table that is causing the problem.

Let me know how you go,

DK
 
Upvote 0
Hi Dk

Unfortunately not - it still errors at the "pfi.Visible = True" statement.:(

As pfi.Visible = False works, I also tried making each item hidden and then making it visible to see if that would slip through the trap, but to no avail.

Thanks for the idea.
 
Upvote 0
Hi Dk

Unfortunately not - it still errors at the "pfi.Visible = True" statement.:(

As pfi.Visible = False works, I also tried making each item hidden and then making it visible to see if that would slip through the trap, but to no avail.

Thanks for the idea.

Are you responding to my post about creating a new pivot table? Your post is the same as before....
 
Upvote 0
what about?
Code:
Sub maybe()
 
  Dim pt As PivotTable
  Dim pf As PivotField
  Dim pi As PivotItem
  Dim wks As Worksheet
 
  Application.ScreenUpdating = False
 
  For Each wks In ActiveWorkbook.Worksheets
    For Each pt In wks.PivotTables
      'set to manual update for speed
      pt.ManualUpdate = True
      'ensure there are no old ghost items in the item lists
      pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
      pt.PivotCache.Refresh
 
      For Each pf In pt.PivotFields
        If pf.Orientation <> 0 Then
          If pf.Orientation = xlPageField Then
            pf.CurrentPage = "(All)"
          Else
            For Each pi In pf.PivotItems
              pi.Visible = True
            Next pi
          End If
        End If
      Next pf
 
      pt.ManualUpdate = False
    Next pt
  Next wks
 
  Set pi = Nothing
  Set pf = Nothing
  Set pt = Nothing
  Set wks = Nothing
 
End Sub
 
Upvote 0
DK

No - I've got myself messed up here using the browser Back button which caused the duplication. I was in the middle of trying to delete it when your latest post came through.

I keep getting timed out after a couple of minutes and have to login every time I want to post. I right royal pain in the a...!

It may take me a while to do the test you've suggested, as I have a few things on my plate right now, but will get back some time today.
 
Upvote 0
Interesting!

I copied the data to a new workbook, and then rebuilt the pivot table.

Both lots of code (ex DK & Fazza) worked on the new pt, as does my Attempt2. (My Attempt1, using pf.ShowAllItems = True, must be an inappropriate method for what I'm trying to achieve, as it either blows the memory :oops: or changes the pt in ways I don't want!)

So it seems that there is something amiss in the original pivot table - but what?:confused:

I have this setup in about six other workbooks, so I may have to rebuild the pt in each one - grrgh!:(
 
Upvote 0
Hi

Do any of your pivot fields have the Autosort option set? I was playing around and have found that if I set the Autosort option (say to Ascending) then the macro fails with the error message "Unable to set the Visible property of the PivotItem class". Let me know if that's the case with your original pivot table - if so there may be a workaround available.

By the way, the ShowAllItems property relates to when you double-click a pivot field and tick the "Show Items with No Data" checkbox.

HTH
DK
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

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