• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Undo in VBA

If you mean "once a VBA program has run, will an Undo command in Excel work?", the answer as far as I've ever experienced is No. It's as if all history has been erased as soon as I run a VBA program, and no Undo has any effect.

But vietm has the right of it if you mean you don't like what you did while you were recording the macro in the first place. In that case, you can record the macro again, not making any mistakes this time. But in my experience it's better just to go over the code it created and modify it to work the way you want.

I'm pretty confident that all the experienced VBA programmers here will agree with me on this: I never use a macro directly after recording it anyway. The macro recorder creates statements 'way too literally, and I have to modify it to make it work to my satisfaction. For instance, say I want to remember how to insert two rows in my worksheet. I record a macro which records something like this:
Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("H1936:H1937").Select
    Selection.EntireRow.Insert
End Sub
But what I want to do is insert two rows wherever my cursor happens to be, not in those rows specifically. So I cut out those statements and insert something closely related into my program. Maybe by then I already had the correct row identified in a variable called vr; if so I might put this in my program:
Code:
Range(ows.Cells(vr, 1), ows.Cells(vr + 1, 1)).EntireRow.Insert
That's all I really cared about.
 
Back
Top