Wanted: VBA code to "show all" (remove filters) in a pivot table

mick0005

Active Member
Joined
Feb 21, 2011
Messages
406
Wanted: VBA code to "show all" (remove filters) in a pivot table.

Good afternoon everyone! I am hoping someone could provide some VBA code which will effectively remove all of the filters (or show all) data in a pivot table. The problem is that on a pivot table in excel 2003, you need to go in and unfilter (click show all) on every field in order to get the data back to being whole. (and excel doesn't even make filtered fields filter arrows blue for pivot tables!!)

I find all of the time people use the spreadsheet and then don't remove the filters before they save. When others go in, they don't realize that the pivot table is missing data from the original source because it is filtered.

I plan on assigning the macro to a button, so it doesn't need any code to execute it at a special time.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Sub Pivot_ShowAll()

    Dim pt As PivotTable, pf As PivotField, pi As PivotItem
    
    Application.ScreenUpdating = False
    
    For Each pt In ActiveSheet.PivotTables
        pt.ManualUpdate = True
        For Each pf In pt.PivotFields
            For Each pi In pf.PivotItems
                pi.Visible = True
            Next pi
        Next pf
        pt.ManualUpdate = False
    Next pt
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Code:
Sub Pivot_ShowAll()

    Dim pt As PivotTable, pf As PivotField, pi As PivotItem
    
    Application.ScreenUpdating = False
    
    For Each pt In ActiveSheet.PivotTables
        pt.ManualUpdate = True
        For Each pf In pt.PivotFields
            For Each pi In pf.PivotItems
                pi.Visible = True
            Next pi
        Next pf
        pt.ManualUpdate = False
    Next pt
    
    Application.ScreenUpdating = True
    
End Sub

Thanks so much for the help. I am encountering an error when trying to use it. I have posted the screenie of the error below.

6-3-201111-42-31PM.jpg
 
Upvote 0
I have one data field, used as a count for verification purposes with the data we're using. The field is actually titled "data".
 
Upvote 0
It was DATE fields I was interested in (not DATA fields) ;)

There's a bug in Excel which means you can't access or amend the Visible property for Row Fields (possibly Column fields) which are of Data datatype (and Date formatted).

To get around this (and get AlphaFrog's code to work) you could temporarily convert the date fields of your Pivot Table (ie Start Date and End Date to a number format) and then run Alpha's code then change them back to dates afterwards.

eg try this:

Code:
Sub Pivot_ShowAll()
 
    Dim pt As PivotTable, pf As PivotField, pi As PivotItem
 
    Application.ScreenUpdating = False
 
    For Each pt In ActiveSheet.PivotTables
        pt.ManualUpdate = True
        'On Error Resume Next   '==== reqd if more than one pivot table in sheet and does not have Start and End Date fields
        pt.PivotFields("Start Date").NumberFormat = "0"
        pt.PivotFields("End Date").NumberFormat = "0"
        'On Error Goto 0    '====reqd as above
        For Each pf In pt.PivotFields
            For Each pi In pf.PivotItems
                pi.Visible = True
            Next pi
        Next pf
        'On Error Resume Next   '=== Ditto
        pt.PivotFields("Start Date").NumberFormat = "dd/mm/yyyy"
        pt.PivotFields("End Date").NumberFormat = "dd/mm/yyyy"
        'On Error Goto 0   '=== Ditto
        pt.ManualUpdate = False
    Next pt
 
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
It was DATE fields I was interested in (not DATA fields) ;)

There's a bug in Excel which means you can't access or amend the Visible property for Row Fields (possibly Column fields) which are of Data datatype (and Date formatted).

To get around this (and get AlphaFrog's code to work) you could temporarily convert the date fields of your Pivot Table (ie Start Date and End Date to a number format) and then run Alpha's code then change them back to dates afterwards.

eg try this:

Code:
Sub Pivot_ShowAll()
 
    Dim pt As PivotTable, pf As PivotField, pi As PivotItem
 
    Application.ScreenUpdating = False
 
    For Each pt In ActiveSheet.PivotTables
        pt.ManualUpdate = True
        'On Error Resume Next   '==== reqd if more than one pivot table in sheet and does not have Start and End Date fields
        pt.PivotFields("Start Date").NumberFormat = "0"
        pt.PivotFields("End Date").NumberFormat = "0"
        'On Error Goto 0    '====reqd as above
        For Each pf In pt.PivotFields
            For Each pi In pf.PivotItems
                pi.Visible = True
            Next pi
        Next pf
        'On Error Resume Next   '=== Ditto
        pt.PivotFields("Start Date").NumberFormat = "dd/mm/yyyy"
        pt.PivotFields("End Date").NumberFormat = "dd/mm/yyyy"
        'On Error Goto 0   '=== Ditto
        pt.ManualUpdate = False
    Next pt
 
    Application.ScreenUpdating = True
 
End Sub

Sorry I read that as DATA.

I tried the new code and I got the below error, then ran debug and have posted both screenshots.

6-6-201111-54-16AM.jpg




6-6-201111-54-32AM.jpg
 
Upvote 0
I wasn't aware of the "bug" that Richard pointed out.

Maybe try something like this.

Code:
Sub Pivot_ShowAll()
 
    Dim pt As PivotTable, pf As PivotField, pi As PivotItem
    Dim strDateFormat As String
 
    Application.ScreenUpdating = False
 
    For Each pt In ActiveSheet.PivotTables
        pt.ManualUpdate = True
        For Each pf In pt.PivotFields
            If pf.DataType = xlDate Then
                strDateFormat = pf.NumberFormat
                pf.NumberFormat = "0"
            End If
            For Each pi In pf.PivotItems
                pi.Visible = True
            Next pi
            If pf.DataType = xlDate Then pf.NumberFormat = strDateFormat
        Next pf
        pt.ManualUpdate = False
    Next pt
 
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
I wasn't aware of the "bug" that Richard pointed out.

Maybe try something like this.

Code:
Sub Pivot_ShowAll()
 
    Dim pt As PivotTable, pf As PivotField, pi As PivotItem
    Dim strDateFormat As String
 
    Application.ScreenUpdating = False
 
    For Each pt In ActiveSheet.PivotTables
        pt.ManualUpdate = True
        For Each pf In pt.PivotFields
            If pf.DataType = xlDate Then
                strDateFormat = pf.NumberFormat
                pf.NumberFormat = "0"
            End If
            For Each pi In pf.PivotItems
                pi.Visible = True
            Next pi
            If pf.DataType = xlDate Then pf.NumberFormat = strDateFormat
        Next pf
        pt.ManualUpdate = False
    Next pt
 
    Application.ScreenUpdating = True
 
End Sub

AlphaFrog - Thanks again for the attempt. I have posted the error I received and debug when I tried running with your new code. Any ideas?

6-6-20112-34-27PM.jpg




6-6-20112-34-08PM.jpg
 
Upvote 0

Forum statistics

Threads
1,215,476
Messages
6,125,029
Members
449,205
Latest member
Eggy66

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