VBA Undo

Folks,
I would like to create a module when called undo's a users action. I plan to use an ActiveX command button to call the module for that worksheet instead of selecting Undo from the Quick Access Toolbar. Creating the module, command button and calling the module is not an issue - just the module code.
Frank FreeseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Here's a piece of code I just found on the web.

    If MsgBox("No = Undo last change", vbYesNo, "Undo") = vbNo Then
        Application.EnableEvents = False
        Application.Undo
        Application.EnableEvents = True
    End If

Open in new window

0
Frank FreeseAuthor Commented:
To begin with I created a module named ChangeToValue():

Sub ChangeToValue()
Dim rng As Range
    With ActiveSheet
    For Each rng In .UserRange
        rng.Value = rng.Value
        Next rng
    End With
End Sub

Open in new window


The purpose of this is code is to delete all occurrences of a formula and replace the formula with the values from the results of the formula. I know I could copy the range I'm interested in and Paste with values.  

The instructions state "The code changes all cells with formulas to cells containing values. Note that all formulas will be deleted. This is common need when copy tables where we need just the results of a calculation and no formulas or individual formatting."

This code is not working to begin with.
0
Martin LissOlder than dirtCommented:
It's UsedRange not UserRange.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Martin LissOlder than dirtCommented:
And it's a small point but for clarity IMO the indenting should be like this

Dim rng As Range
    With ActiveSheet
        For Each rng In .UsedRange
            rng.Value = rng.Value
        Next rng
    End With
End Sub

Open in new window

0
Frank FreeseAuthor Commented:
I just caught that coding error myself - now the first code works
I then tried the code you suggested to Undo and it failed at "Application.Undo"
Got a "Run time error 1004" with "Method 'Undo of object'_Application failed
0
Martin LissOlder than dirtCommented:
That's what happens when you copy code and don't test it.

Change line 1 in my first post to

If MsgBox("No = Undo last change", vbYesNo, "Undo") = vbYes Then
0
Frank FreeseAuthor Commented:
That did not work, but Martin, you gave me an idea I'd forgotten - the MsgBox. I've noticed how complicated the code can be to Undo. If you can come up with an easy way let me know and I'll re-post my question. But you have helped with the MsgBox option
0
Frank FreeseAuthor Commented:
thanks - you were a big help
0
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.