Link to home
Start Free TrialLog in
Avatar of Frank Freese
Frank FreeseFlag for United States of America

asked on

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.
SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Frank Freese

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
thanks - you were a big help
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013