Clear all pivot table fields with a Macro

H

Harry Flashman

I have a two things I would like to do, ideally with a macro

1. Clear all fields from a pivot table
2. If any of the fields have been filtered - remove the filters

I am hoping there is a simple command or two that I have not yet
discovered.

To put things into perspective: I have multiple pivot tables (all
copies of the original table, and each on their own spreadsheet).
My plan is to have each table set up for particular queries (that is
with different fields and filters). I will then examine the data on
each, and sometimes alter the fields as needed. But when I am done I
want to press a button and return each table to its origal state.

If each of the tables are blank to begin with, I think I can use the
macro recorded to automate setting them up.
So what I think I need now is a macro to remove all fields and
filters.
I would greatly appreciate any suggestions.
Cheers,
 
H

Harry Flashman

Well I think I am nearly there. After some more searching and then
playing around with what I have found I have come up with this:
It removes the fields, but not the fitlers, but it seems to work. Now
if I could only find a way to clear all filters.

Sub RemoveDataFields()
On Error Resume Next
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ActiveSheet.PivotTables(1)


For Each pf In pt.DataFields
pf.Orientation = xlHidden
Next pf
For Each pf In pt.RowFields
pf.Orientation = xlHidden
Next pf
For Each pf In pt.ColoumnFields
pf.Orientation = xlHidden
Next pf
For Each pf In pt.PageFields
pf.Orientation = xlHidden
Next pf

End Sub
 
H

Harry Flashman

Well I solved my problem (it seems to work anyway)

To clear all the filters I add the following line to my macro (shown
in my previous post)

ActiveSheet.PivotTables(1).ClearAllFilters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top