Pivottable, remove obsolete items

Tommy Bak

Active Member
Joined
Feb 25, 2002
Messages
288
Hi
does anyone know how to get rid of obsolete / missing items. ?
If the database is updated and the pivottable is refreshed, the dropdownboxes still contains items that has been removed from the database.
Is it possible to update these items


Regards
Tommy Bak
 
This is what I ended up with.
It takes into account that some items may be calculated items, which will have a count of 0
Thank to all who helped me on this one.
If anyone have knowledge of a smarter way, please inform me



Code:
Sub CleanPivotItem()
Dim PT As PivotTable
Dim PF As PivotField
Dim PI As PivotItem

For Each PT In ActiveSheet.PivotTables
    For Each PF In PT.PivotFields
        For Each PI In PF.PivotItems
            If PI.RecordCount = 0 And PI.IsCalculated = False Then PI.Delete
        Next PI
    Next PF
Next PT

End Sub

regards
Tommy Bak
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi,

Seems like you have sorted your problem Tommy but I thought I'd throw in my two pennies worth:-

1. Drag the field in question off of the pivot table.
2. Refresh the pivot table.
3. Drag the field back to its previous location. The deleted items should have disappeared from the drop-down.

This is still a labourious way of fixing this but is probably easier than re-creating the pivot table from scratch.
 
Upvote 0
Hi Dan
That what I would do normally (when I used xl97), but it doesn't seem to work in XP
Thank you anyhow :beerchug:

regards
Tommy Bak
 
Upvote 0
I really wish I would have scrolled to Page 2 of this sooner! I basically recreated what Tommy did on my own...

However, I also ran into some errors, so here's what I ended up with:

<font face=Courier New>

<SPAN style="color:#00007F">Sub</SPAN> CleanPivotItems()

    <SPAN style="color:#00007F">Dim</SPAN> pt <SPAN style="color:#00007F">As</SPAN> PivotTable, pf <SPAN style="color:#00007F">As</SPAN> PivotField, pi <SPAN style="color:#00007F">As</SPAN> PivotItem
    <SPAN style="color:#00007F">Dim</SPAN> booShowMsg <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, mbxResult <SPAN style="color:#00007F">As</SPAN> VbMsgBoxResult
    
    booShowMsg = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrorHandler
    
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> pt <SPAN style="color:#00007F">In</SPAN> ActiveSheet.PivotTables
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> pf <SPAN style="color:#00007F">In</SPAN> pt.PivotFields
            <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> pi <SPAN style="color:#00007F">In</SPAN> pf.PivotItems
                <SPAN style="color:#00007F">If</SPAN> pi.RecordCount = 0 And pi.IsCalculated = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
                    Debug.Print "Trying to delete - "; pi.Name; " "; pi.RecordCount; " Parent = "; pf.Name
                    pi.Delete
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">Next</SPAN> pi
        <SPAN style="color:#00007F">Next</SPAN> pf
    <SPAN style="color:#00007F">Next</SPAN> pt
    <SPAN style="color:#00007F">Set</SPAN> pi = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> pf = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> pt = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

ErrorHandler:

    <SPAN style="color:#00007F">If</SPAN> booShowMsg <SPAN style="color:#00007F">Then</SPAN>
        mbxResult = MsgBox("Macro could not delete item." & vbCr & _
                           "Likely cause is that at one time this item was part of a group." & _
                           vbCr & vbCr & "Click [CANCEL] to supress further Error messages.", _
                            vbExclamation + vbOKCancel, "Error Deleting PivotItem")
        booShowMsg = (mbxResult = vbOK)
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Fun problem Tommy. (100% credit to Tommy for the IsCalculated check - I totally missed that!)
 
Upvote 0
Greg -> I like your errorhandler.
I also found that the macro stopped at strange places, in some pivots. Especially where there were empty() in the dropdownbox.
I will adopt this macro... :-)

regards Tommy
 
Upvote 0
Tommy,

Someone posted the exact same question on 12/31. I xref'd this post but Mike (aka Ekim) xref'd a different URL that had some very helpful ideas.

Based on info from that post - I did the following - which handled my grouping trouble-makers without erroring. You might try it on your troublemakers and see if it stands up to them. I don't know how this responds to calculated fields (I've yet to need them).

<font face=Courier New><SPAN style="color:#007F00">'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯</SPAN>
<SPAN style="color:#00007F">Sub</SPAN> CleanPivotItems2()
<SPAN style="color:#007F00">'______________________________________________________________________________</SPAN>

<SPAN style="color:#007F00">' ***********************</SPAN>
<SPAN style="color:#007F00">' ***** XL2002 ONLY *****</SPAN>
<SPAN style="color:#007F00">' ***********************</SPAN>

<SPAN style="color:#007F00">' Even after a Refresh, a PT retains options for checking / unchecking</SPAN>
<SPAN style="color:#007F00">' that are no longer part of the source data set.  This macro goes</SPAN>
<SPAN style="color:#007F00">' through all pivot tables on the active sheet and cleans off</SPAN>
<SPAN style="color:#007F00">' any PivotItems that no longer have corresponding records in the source</SPAN>
<SPAN style="color:#007F00">' data block.</SPAN>

<SPAN style="color:#007F00">' Note: This only needs to be run once.  After .MissingItemsLimit has</SPAN>
<SPAN style="color:#007F00">'       been set to xlMissingItemsNone, a simple REFRESH will remove dead</SPAN>
<SPAN style="color:#007F00">'       checkbox items.</SPAN>

    <SPAN style="color:#00007F">Dim</SPAN> pt <SPAN style="color:#00007F">As</SPAN> PivotTable

    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> pt <SPAN style="color:#00007F">In</SPAN> ActiveSheet.PivotTables
        pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
        pt.PivotCache.Refresh
    <SPAN style="color:#00007F">Next</SPAN> pt
    
    <SPAN style="color:#00007F">Set</SPAN> pt = <SPAN style="color:#00007F">Nothing</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

Regards,
 
Upvote 0
Greg
Thankyou for the link and the code.
Seem that we were on the right track in xl2000 :-)

regards
Tommy
 
Upvote 0
Hi All

This can be done in following way without deleting Pivot tables.

1. Go back to query again and give another alias name for the field

2. Say for example the field name which you have origianlly is Year in the query. Now change this in query and give a new alias name as Yr,

3.When you return back to Pivot table, now field Yr will hold only latest items and will not show obsolete items.

4. Field Yr can again be changed back to Year in Pivot table by editing it.
 
Upvote 0
I've got to get home, but this appears to work if I go in by hand and uncheck the Show All box and then check one box for a record that I know still exists...

Sub DeletePivotItemTest()
****Dim pi1 As PivotItem
****On Error Resume Next
****For Each pi1 In ActiveSheet.PivotTables("PivotTable2").PivotFields("Rep").PivotItems
********If pi1.Visible = False Then pi1.Delete
****Next pi1
End Sub


I was so much stuck.. This helped me a lot thanks :):)
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,643
Members
452,663
Latest member
MEMEH

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