Corrupt files due to pivot remnants after saving via VBA

Carl Colijn

New Member
Joined
Jun 23, 2011
Messages
32
Hi all,

I've got an interesting but complicated question here. The context is Excel 2007 (with SP2 - 12.0.6545.5004) on Windows XP; the workbook has been build from scratch in Excel 2007 (i.e. no converted 2003 pivots).

I have created a workbook which users can export derived (data-filtered) workbooks from. The workbook contains pivots based on hidden data sheets, but when the data filtering would result in no records being left for a pivot I remove the pivot altogether (since a pivot cannot be based on zero records).

Once in a while the beta-tester users report that the generated filtered workbooks are corrupt; when opening these files Excel asks:
"Excel found unreadable content in workbook "<WORKBOOK'S name>". Do you want to recover the contents of this workbook?"
When selecting "Yes" Excel tells me:
"Removed Records: PivotTable report from /xl/workbook.xml part (Workbook)"
and the resulting workbook is totally OK.

I finally laid my hands on one of the workbooks that gave rise to this error. In this case the exporting functionality very infrequently also causes a
"Automation error - the object invoked has disconnected from its clients"
after which Excel just crashes. I have not yet pinpointed where this crash occurs; after adding file logging the problem hasn't re-occured yet...

By eliminating sheets and pivots I found out the pivots are at fault. The stripped workbook contains just one sheet with one pivot on it, and an item in the pivot is selected. When I then run the code as-is, the resulting workbook will be corrupt. But when I select a cell outside the pivot and then re-select the same cell in the pivot, the code works flawlessly!

Since I couldn't troubleshoot it further from there I saved both the problematic version and the "cured" version of the workbook, unzipped both and did a diff on their contents. The only relevant change is that in the XML for the sheet containing the pivot the "sick" version contains the fragment:
HTML:
<sheetView tabSelected="1" workbookViewId="0">
  <selection activeCell="A5" sqref="A5"/>
  <pivotSelection pane="bottomRight" showHeader="1" click="1" r:id="rId1">
    <pivotArea dataOnly="0" labelOnly="1" fieldPosition="0">
      <references count="1">
        <reference field="1" count="0"/>
      </references>
    </pivotArea>
  </pivotSelection>
while the "cured" version misses the pivotSelection tag completely. Note that cell A5 is located in the pivot table.

In this case the filtering done by the export removes this pivot table completely (there would be no data left). Comparing the resultant exported workbooks from both the cured and sick versions I found that BOTH contain the same pivotSelection tag, thus also the one where the pivot has actually been removed?! It seems as though the pivotSelection info has creeped into the sick version, and once there it wants to stay there, causing an error if there is no accompanying pivot table anymore.

So my question is: what causes the above XML tag to appear? Selecting a cell outside the pivot and then again in the pivot only causes this tag to disappear and not re-appear. Doing it via VBA by the way unfortunately didn't do the trick. I now remove the pivot by calling
Code:
Call oPivotSheet.Rows.Delete
(where oPivotSheet only contains the one pivot table). When I add
Code:
Call oPivotSheet.PivotTables(1).ClearTable
in front of that the problem goes away. So for now that is my solution, but what causes these "stray" pivotSelection tags to appear anyway?

Thanks in advance,
Carl Colijn
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Sounds like a tough problem...

Not sure if this helps you in anyway, but pivottables are based on one or more pivotcaches which are maintained by Excel behind the screens. Maybe it's some issue with a pivotcache remaining after deleting the corresponding pivottable?
Although I would expect you to see a file difference somewhere for that too...

Check the object browser for pivotcache information. From memory I think it's workbook based, not worksheet.

Good luck!
 
Upvote 0
Hi Hermanito,

Thanks for the suggestions!

The pivot caches are indeed stored at the workbook level (they're all in a big collection - Workbook.PivotCaches). When I debugged the potentially troublesome code:
Code:
Call oPivotSheet.Rows.Delete
the nr. of caches before was 1 and after it correctly went to 0. So it seems that removing the pivot table by just removing all cells of the sheet the pivot is on also gets rid of the underlying cache. The thing still remaining (in the XML code at least) is the pivotSelection tag, even though there is no pivot on the sheet anymore...

Even when I insert the code
Code:
Call oPivotSheet.Cells(1,1).Activate
Call oPivotSheet.Cells(1,1).Select
both before and after the Sheet.Rows.Delete call, the resulting workbook still becomes corrupt (ie. contains the remnant pivotSelection tag).

Mysterious...
 
Upvote 0
Alas, contrary to what I thought this didn't really fix the problem.

The pivotSelection XML tag in the xlsm source led me to the VBA PivotTable.PivotSelection method (who could have guessed? :) ), which doesn't hold a selection as in which cell is selected, but instead it contains which items are filtered. It was indeed set for the problem pivot table.

When I added:
Code:
Call oPivotSheet.PivotTables(1).ClearAllFilters
in front of the already added PivotTable.ClearTable call, the resulting .xlsm workbook was not corrupt anymore.

It thus seems that when a pivot table is filtered, and when you consecutively remove the table in VBA by deleting all cells on the sheet, the pivot's filtering is remembered behind the scenes, which causes the workbook to be 'corrupt' when you re-open the workbook. This was for Excel 2007, SP 2.

Problem (again) fixed, I hope...
 
Upvote 0
Glad you found it...

I'm now trying to store this somewhere in the back of my mind for when I have the same problem at some time in the future :biggrin:
 
Upvote 0
So far so good, I had to use a combination of the clear filters and clear cache suggestions to stop Excel from crashing on save after deleting pivot tales and to avoid the "Removed Records: PivotTable report from /xl/workbook.xml part
(Workbook)" issue on workbook reopen. (Sorry for any coding issues--I'm a newbie.)

Code:
Sheets("Pivot Table").Activate

With ActiveSheet.PivotTables("pvtCompletedOfferings")
'see http://www.mrexcel.com/forum/excel-questions/559331-corrupt-files-due-pivot-remnants-after-saving-via-visual-basic-applications.html
     .ClearAllFilters
     .ClearTable
End With

'see http://www.contextures.com/xlPivot04.html
'"DeleteMissingItems2002All" procedure for clearing cache from contextures.com
'i.e.,  for each pt in ws, pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
'and, for each pc In wb.PivotCaches pc.Refresh

DeleteMissingItems2002All

Range("A1:C18").Select
Selection.ClearContents
Selection.Delete Shift:=xlToLeft
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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