I have a pretty simple bit of code that I'm having trouble with. All it is supposed to do is Undo the last action, then format a target cell. If it is run twice in succession, I get the error - Run-time error '1004' undo of object _application failed. For instance, if I type the word "test" in a cell, then run the macro, it will remove the word from the cell (revert to the previous state). If I run it again without typing anything new on the sheet, I get the error.
It's okay if I can't actually Undo two consecutive actions, but is there something I can put in to avoid the error? Ideally, I would type the word "test" in a cell, then run the macro and it removes the word. Then run it a second time and nothing happens (or, better yet, the interior color change still happens).
I hope this makes sense. It is for a little proof of concept I need to have done.
Thank you all for your help.
Code:
Sub Undo()'
' Undo Macro
'
Application.Undo
Range("C4").Interior.ColorIndex = Int((44 - 1 + 1) * Rnd + 1)
'
End Sub
It's okay if I can't actually Undo two consecutive actions, but is there something I can put in to avoid the error? Ideally, I would type the word "test" in a cell, then run the macro and it removes the word. Then run it a second time and nothing happens (or, better yet, the interior color change still happens).
I hope this makes sense. It is for a little proof of concept I need to have done.
Thank you all for your help.