Solved

VBA is Clearing Excel’s Undo Stack – In Reference to:  Worksheet_PivotTableUpdate

Posted on 2014-04-29
5
822 Views
Last Modified: 2014-04-29
When one of our Users updates a pre-written Excel OLAP Pivot Table report, we are using the VBA Event “Worksheet_PivotTableUpdate” to kick off a procedure that writes a Usage Log record to SQL Server.   When the PivotTableUpdate event runs, it clears Excel’s UNDO stack and the user does not have the ability to Undo the pivot table update.   I am looking for a solution that will allow the VBA Usage Log routine to run but still allow for the Pivot update to be reversed with Undo.

One available option is to eliminate running the PivotTableUpdate Event but we would like to run our Usage Log routine if we can find a way around this current hurdle.

Thank you for any suggestions and assistance,
Jerry
0
Comment
Question by:Jerry Paladino
  • 2
  • 2
5 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 500 total points
ID: 40030372
As it happens, the Undo stack is cleared by design whenever a VBA macro runs. This is a "feature" and not a "bug".

The best way around it would probably be the following:
1.  Use the event sub to turn events off, capture the current state of the PivotTable, use Application.UnDo and then capture the previous state of the PivotTable in a Public array variable, then restore the PivotTable and perform your logging. Finally, turn events back on.
2.  If the user wants to Undo the update, he needs to run a separate macro that applies the previous state from the Public array variable.

Potential problems: the PivotTable formatting won't be captured in the Public array variable, so the post-restoration results may look different--though they will have the desired values.

Note: to declare a variable as Public, you do so before any of the subs or functions in the module sheet. I suggest making that declaration in a regular module sheet so the variable is readily available to both event subs and regular subs.
Public PivotTableUndo As Variant

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40030382
Running vba will always clear the undo stack.
0
 
LVL 16

Author Comment

by:Jerry Paladino
ID: 40030464
Thank You Brad...   Yes, I understand this is a VBA feature and did not view it as a bug.   Was just looking for a way to accomplish both if possible.  

Something like intercepting the Pivot Table call before it updated the pivot table, call the Usage Log routine prior to the Pivot Update and then let the pivot table update.   That would complete the VBA before the pivot updated and should leave the Undo stack in place to return it to original state.   I know there is not one, but something like a BeforePivotTableUpdate event.  Could that be done in a class module?

I understand and appreciate your suggestion on capturing the state of the pivot table and then putting it back together after the update.   The formatting is one issue and would require a lot of code to put things back to original state for 20+ standard reports. The other is the time required to process the OLAP report again on rebuild.

Thanks,
Jerry
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 40030544
Jerry,
Excel 2013 has six different PivotTable event subs available to worksheet code panes. You are using the last to occur in the sequence. I haven't ever used it, but PivotTableBeforeCommitChanges is supposed to run before changes are committed to an OLAP server, and so may be able to substitute for the non-existent "PivotTableBeforeUpdate".

Brad
0
 
LVL 16

Author Closing Comment

by:Jerry Paladino
ID: 40030762
Brad,

Thanks Again...   I am having a little trouble getting the Worksheet_PivotTableBeforeCommitChanges Event to fire so I'll do a bit more research on that one.   Between the two options you provided I can make this work.   I appreciate your help.

Jerry
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question