0

For the past several months I've been writing VBA scripts, and have noticed that, if I write a module that acts upon an Excel workbook/sheet, I'm not able to Ctrl+Z these actions. This, from what I've noticed, applies to any and every action that's performed by the module.

A simple example would be:

Sub test()
  Cells(1,1) = "Hello World"
End Sub

Why can't module actions be "reversed"? i.e., undone through Ctrl+Z, or, by utilizing the 'undo' arrow.

P.S. I think this behavior might also apply to any module action within any MS application, though, I could easily be wrong.

  • 2
    Because they're not *user actions* and thus not added to the application's *undo buffer*? Because *that's how Microsoft implemented it*? What do you expect the answer will look like? Do you expect the Microsoft Excel dev team to post an answer here? – Mathieu Guindon Aug 28 '17 at 19:37
  • 1
    Possible duplicate of ["Undo" history button clear after run macro excel](https://stackoverflow.com/questions/7798575/undo-history-button-clear-after-run-macro-excel) – dwirony Aug 28 '17 at 19:38
  • 1
    @Mat'sMug I have no expectation of what an answer to this question should look like. If actions are only added to the application's undo buffer when a user directly performs the action, I suppose that sufficiently answers my question..! Thanks. –  Aug 28 '17 at 19:40
  • @dwirony I don't think it's a duplicate of that post, since the OP in that post is asking why other undo actions are cleared. My question focuses on why module actions can't be undone all together. Possibly a technical differentiation, but I think it's different enough. –  Aug 28 '17 at 19:42
  • The explanation is the same, if I hadn't already voted to close as *primarily opinion-based* I'd have seconded @dwirony's finding and hammer-closed as a duplicate. "Why does it work that way" is impossible to answer with anything other than "because that's how it is". Hence, 'tis the same question, with the same answer. – Mathieu Guindon Aug 28 '17 at 19:44

1 Answers1

0

No they can't be reversed but you can use my method:

You could save your workbook (ActiveWorkbook.Save) at beginning of each macros then if you want to undo:

Const pathToTheFile as String = "C:\Users\VbaProject\"
Sub Undo()

    wb = ActiveWorkbook.Name

    Workbooks(wb).Close savechanges:=False
    Workbooks.Open Filename:=pathToTheFile & wb

End Sub

You can even bind it to a shortcut...

exSnake
  • 682
  • 1
  • 8
  • 26