Routine to Delete Old PivotTable List Values

Scott in Phoenix

New Member
Joined
Mar 5, 2002
Messages
13
The fact that PivotTable Item Lists never update has bugged me forever. Well, the other day I got sick of it. We changed every value to upper case on a huge data sheet the other day. Rather than manually hide hundreds of Items, I got fed up and found the code below - which works!

How can I modify this code so it cleans every PivotTable in the active workbook? Right now, I have to change the worksheet name and the name of the PivotTable I want cleaned.

I could probably kluge something together and eventually get it to work. But I'd like something clean that I can add to my personal.xls and fire off anywhere anytime I want.

Code:
Sub Delete_Fields()
   On Error Resume Next
   For Each pvtfield In Worksheets("worksheetname").PivotTables("PivotTablename").PivotFields
      For Each pvtitem In pvtfield.PivotItems
         pvtitem.Delete
      Next
   Next
   ActiveSheet.PivotTables("PivotTablename").RefreshTable
End Sub


Thanks in advance!

Scott in Phoenix
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
There is Your code...

' This code deletes old items in all pivot tables in active workbook.
Sub Delete_Old_Pivot_Items()
On Error Resume Next
For Each WrkSheet In Worksheets
For Each PvtTable In WrkSheet.PivotTables
For Each PvtField In PvtTable.PivotFields
For Each PvtItem In PvtField.PivotItems
PvtItem.Delete
Next: Next: Next: Next
End Sub
 
Upvote 0
Cool!

Hey - thanks for saving me all the trouble of trying to figure this out for myself. I'm at that state where I can see why your code works and how it does what it does, but have noooo clue how to make some of the things myself...

Thanks again!

Scott
 
Upvote 0
There's an oldy to be dug up.
I'm running Xl 2007 in compatability mode. I have a pivottable linked to external data in a Access data base.

The pivottable remembers previous feilds and data that has been replaced by new feilds and data by linking to a different database

Tried the code as suggested but it doesn't seem to work on all feilds.
Have I got the order of doing things wrong?
Run the code then refresh the pivot?

Any idea?

ziggy
 
Upvote 0
Too easy.

Really good site to.
Also discovered that there were some comma's in my data that pushed everything along a couple of columns.

cheers
ziggy
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,817
Members
449,049
Latest member
cybersurfer5000

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