Solved

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

Posted on 2014-04-29
5
870 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

623 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