Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-29
5
Medium Priority
?
922 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 2000 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 28

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

783 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