Learn Excel - Not Every Macro Clears Undo - Podcast #1904

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Aug 7, 2014.
I used to believe that every Excel macro would clear the undo stack. But, as you will see today, sometimes a macro can run and the Undo stack remains in place.
maxresdefault.jpg


Transcript of the video:
The MrExcel podcast is sponsored by Easy-XL.
Learn Excel from MrExcel podcast, episode 1904.
Every Macro Does Not Clear UnDo Stack!
This is kind of a follow up to yesterday's podcast, episode 1903, where I showed that we have this event handler macro, that, when we would change something from No to Yes in the Paid column, that record was automatically being written to the data column.
And I was going to record an addendum to that podcast yesterday, warning that the problem with these event handler macros, let me show you the event handler macro, is that, you know, traditionally the belief is that every time we run a macro, we clear the undo stack, all right.
So that means, that every time that something changes on this worksheet, this macro runs and then therefore we lose undo.
And so to demonstrate that, I was going to come here and delete some stuff and then come here and overwrite some stuff.
And then show you that when I press Ctrl+Z, it would not be able to undo.
But here, watch, I'm going to Ctrl+Z and undoes, and Ctrl+Z and undoes again, what the heck is up with this?
Were those two changes I made not enough to cause the event handler to fire?
So we'll come over here and let's just add a message box: Msgbox “Yep, I am running!”, all right.
And then we'll come back here, just choose some records, press Delete and sure enough, it is running the macro.
But Ctrl+Z, we still have “undo”.
What's up with that?
So, as I dug in and did some more research, it turns out that the rule is: the undo stack is cleared when your macro writes something to the worksheet.
If the macro does some analysis and doesn't write to the worksheet, then the undo stack is not cleared.
So in this particular macro, it is only doing something, if the change happened in column 7.
And if the change happened to be Yes, so if we had changed any other column, other than column G, and if we wrote anything other than a lowercase or capital Y, this macro isn't going to make any changes and therefore the undo stack stays intact.
Now, I had been under the wrong impression for a long time, that any macro would clear the undo stack.
But it turns out, if you have a macro that just does some analysis and does not change the worksheet, the undo stack remains intact.
Alright, I want to thank you for stopping by, we'll see you next time for another netcast – MrExcel.
 

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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