Application.Undo not working properly!!!

vini1508

New Member
Joined
Feb 8, 2016
Messages
29
i have form for change case.. it contains RefEdit control , option button, command button....
1. by using RefEdit control I'm selecting Range..
2. Option button is for selecting Upper Case..
3. On click on command button the selected range value is change to Upper Case..

this is working fine...
but now i wanted to undo the action done by userform so i have used Application.Undo.. undo button is getting activated but it is not undoing the action plss help.. code I'm using is below...

Code:
Private Type SaveRange
    Val As Variant
    Addr As String
End Type
Public OldWorkbook As Workbook
Public OldSheet As Worksheet
Private OldSelection() As SaveRange


Dim b, c
Dim i, j, k As Long


Private Sub btn_change_Click()
    Set b = ActiveSheet.Range(ref_range.Text)
    ReDim OldSelection(b.Cells.Count)
    Set OldWorkbook = ActiveWorkbook
    Set OldSheet = ActiveSheet
    j = 1
    If Application.CountBlank(b) = b.Cells.Count Then
        MsgBox "Empty Range"
        ref_range.SetFocus
    Else
        If opt_upper Then
            For Each c In b
                If c.Value <> "" Then
                    c.Value = UCase(c.Value)
                    OldSelection(j).Addr = c.Address
                    OldSelection(j).Val = c.Formula
                    j = j + 1
                End If
            Next
        End If
        ref_range.SetFocus
    End If
    Application.OnUndo "Undo the Change Case", "UndoCase"
End Sub

Sub UndoCase()
    On Error GoTo Problem
    Application.ScreenUpdating = False
    OldWorkbook.Activate
    OldSheet.Activate
    For k = 1 To UBound(OldSelection)
        Range(OldSelection(k).Addr).Formula = OldSelection(k).Val
    Next k
    Exit Sub


Problem:
    MsgBox "Can't undo"
End Sub

Whenever i click Ctrl+Z it gives Msgbox("Can't undo") everytime... pls help
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top