15

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.

Community
  • 1
  • 1
Vivian
  • 1,071
  • 5
  • 16
  • 29
  • There's a hiccup with this, in that when you call the VBA code, the undo stack is destroyed (make a spreadsheet, put some items on it, delete them, then call any VBA code to see what I mean). I'm not sure if there's a way around this. Another user had a similar issue, and I think you'd basically have to come up with a way to write changes to a file, and read them back again. – jonsca Aug 10 '11 at 01:24
  • 1
    Minor point, but it is not true - or is no longer true - that calling _any_ VBA destroys the undo stack. Only VBA macros that make changes destroy the undo stack. – PhilHibbs May 02 '18 at 12:45

1 Answers1

16

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

Reafidy
  • 8,240
  • 5
  • 51
  • 83