Undo action after running code

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I would like to do the following:

1. Enter some data on a worksheet manually
2. Click on a button to run some code
3. Be able to undo action 1.

I came across this article:


but I can't implement it.

I copied the two class modules and created my own simple subroutine:

Code:
Sub Macro1()

Msgbox "Hi"

End Sub

then I entered a value on the worksheet and ran Macro1.

What I expected to happen is if I now clicked undo, that the value I entered on the worksheet to disappear.

Am I mistaken the code on the website is not for this purpose?

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
The code won't work for what you want. The code's purpose is to undo certain actions performed by the VBA. In the implementation example on the website, cells A1:A10 have their color changed. The undo code allows that action to be undone. However, as soon as the first cell has its color changed, the previous undo actions you manually performed would get cleared, leaving the undo list at the end with just the ability to undo the color change. In your example, 1 would get cleared, but you could undo 2 in theory (as long as the actions are supportable like you can't undelete a sheet if the code deletes it).

To implement the code, you'd have to use something like the implementation example. In your code above, you don't take any advantage of having included the classes. You simply show a message box, which shouldn't change the undo history I think since it's not an "undoable" action like deleting something or setting the value of a cell, etc.
 
Upvote 0
The code won't work for what you want. The code's purpose is to undo certain actions performed by the VBA. In the implementation example on the website, cells A1:A10 have their color changed. The undo code allows that action to be undone. However, as soon as the first cell has its color changed, the previous undo actions you manually performed would get cleared, leaving the undo list at the end with just the ability to undo the color change. In your example, 1 would get cleared, but you could undo 2 in theory (as long as the actions are supportable like you can't undelete a sheet if the code deletes it).

To implement the code, you'd have to use something like the implementation example. In your code above, you don't take any advantage of having included the classes. You simply show a message box, which shouldn't change the undo history I think since it's not an "undoable" action like deleting something or setting the value of a cell, etc.
Thanks for your reply.

I'm a little confused. Are you saying in my example, it is IMPOSSIBLE to undo action 1 manually, (after a macro has been run) or are you saying it IS POSSIBLE but I have amended the code correctly?
 
Upvote 0
It is impossible to undo action 1. The undo list that grows when you manually do things on the sheet before running the macro gets cleared when the code starts manipulating things.
 
Upvote 0
Solution
It is impossible to undo action 1. The undo list that grows when you manually do things on the sheet before running the macro gets cleared when the code starts manipulating things.
Thanks for clarifying.
 
Upvote 0

Forum statistics

Threads
1,214,837
Messages
6,121,883
Members
449,057
Latest member
Moo4247

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