Solved

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

Posted on 2014-04-29
5
780 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 80

Assisted Solution

by:byundt
byundt earned 500 total points
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
Running vba will always clear the undo stack.
0
 
LVL 16

Author Comment

by:Jerry Paladino
Comment Utility
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 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now