Is there anyway to code an undo function onto a CommandButton similar to the Excel's very own undo function?
Or a function which is capable of calling Ctrl-Z shortcut key.
As far as undoing the actions of a VBA macro is concerned , it will have to be implemented by the user at the time of writing the code. For example , if you do a worksheet SORT , all that is required to undo the sorting action is to press CTRL Z.
When we run a macro, we cannot undo the macro's actions using the usual Ctrl + Z shortcut. Running a macro removes the list of actions stored in Undo, meaning that it is not possible to undo a macro.
To undo an action press Ctrl+Z. If you prefer your mouse, click Undo on the Quick Access Toolbar. You can press Undo (or CTRL+Z) repeatedly if you want to undo multiple steps.
Remarks. This method undoes only the last action taken by the user before running the macro, and it must be the first line in the macro. It cannot be used to undo Visual Basic commands.
Add the command button to the worksheet and assign the following macro to it:
Sub UndoLastAction()
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
End Sub
It can only undo the last action taken by the user and cannot undo VBA commands.
EDIT: If you need further undo capabilities see:
Undo With Excel VBA - JKP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With