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

Vivian picture Vivian · Aug 10, 2011 · Viewed 73.7k times · Source

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.

Answer

Reafidy picture Reafidy · Aug 10, 2011

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