Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically code an 'undo' function in Excel-Vba?

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.

like image 816
Vivian Avatar asked Aug 10 '11 00:08

Vivian


People also ask

How do you undo code in VBA?

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.

How do I undo a macro in VBA?

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.

How do I undo a code in Excel?

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.

What is application undo?

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.


1 Answers

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

like image 98
Reafidy Avatar answered Oct 21 '22 13:10

Reafidy