Application.Undo

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Can someone please explain when does Application.Undo work?

I have this event on a worksheet:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Call ModUndo.RollBack

End Sub

and this is ModUndo:

Code:
Sub RollBack

    Application.EnableEvents = False

    Application.Undo

    App;ication.EnableEvents = True

End Sub

and it works when a value on Sheet1 is changed.

However if instead the event called another Function first, such as:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If CheckForRoll Then Call ModUndo.RollBack

End Sub

and CheckForRoll contains this:

Code:
Function CheckForRoll() As Boolean

    If Sheet1.Cells(1, 1).Value = 10 Then

        CheckForRoll = True

    Else

        CheckForRoll = False

    End If

End Function

I get an error in ModUndo.

Why is that?

Thanks
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,

Just gave it a go and there were some errors indeed.

In rollback their was a mistype
VBA Code:
App;ication.EnableEvents = True vs Application.EnableEvents = True


and with CheckForRoll:

VBA Code:
If ThisWorkbook.Sheets(1).Cells(1, 1).Value = 10 Then
        CheckForRoll = True
    Else
        CheckForRoll = False
    End If

Should works fine
 
Upvote 0
Hi,

Just gave it a go and there were some errors indeed.

In rollback their was a mistype
VBA Code:
App;ication.EnableEvents = True vs Application.EnableEvents = True


and with CheckForRoll:

VBA Code:
If ThisWorkbook.Sheets(1).Cells(1, 1).Value = 10 Then
        CheckForRoll = True
    Else
        CheckForRoll = False
    End If

Should works fine
Forgot to explain a little, you didnt state which workbook and which sheet to check.

VBA Code:
Sheet1.Cells(1, 1).Value
Should be:
VBA Code:
ThisWorkbook.Sheets(1).Cells(1, 1).Value
 
Upvote 0
@Ryan0120
This Sheet1.Cells(1, 1).Value does specify the sheet & workbook. ;)
As you can only use the codename directly for the workbook containing the code, it's saying ThisWorkbook sheet codename Sheet1
 
Upvote 0
Solution
Hi,

Just gave it a go and there were some errors indeed.

In rollback their was a mistype
VBA Code:
App;ication.EnableEvents = True vs Application.EnableEvents = True


and with CheckForRoll:

VBA Code:
If ThisWorkbook.Sheets(1).Cells(1, 1).Value = 10 Then
        CheckForRoll = True
    Else
        CheckForRoll = False
    End If

Should works fine
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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