Assing key to undo all changes at once

Lux Aeterna

Board Regular
Joined
Aug 27, 2015
Messages
205
Office Version
  1. 2019
Platform
  1. Windows
Hey everyone!

I have created an excel in which I insert blood exam results. I insert every patient manually.

What I want to do is to assign a key (for example F5) to undo all changes made to the sheet at once. I know I can do it using undo button, but I'd like to have a specific key for that.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
VBA can only undo the last change by the user. However, if you understand the nature of the undo, you can mimic the action by planning the interval of changes, normally by using the Save option when you want to commit to your changes. If you make a bunch of changes you need to undo, you can just exit the file without saving and reopen.

Another option would be to copy the sheet as a save-point to which you can revert. This can be programmatic, but you'd need to control when to commit to those changes.
 
Upvote 0
Thank you for your answer!

The second solution seems to suit me better. To be precise, I need to revert to the original sheet just after I have printed my data (so I can enter new data, print again and so on). How can I do it?
 
Last edited:
Upvote 0
Make sure you test this out with a copy of your workbooks as these aren't tested and will delete sheets. The solution is two pronged, one macro to save your backup when the workbook is printed (should work on printing anything within the workbook) and another for when you want to revert back to the last backup.

The first macro should work automatically once placed in your "ThisWorkbook" object and the other can be attached to a button or keyboard shortcut (F5 is already taken by Excel, so it would usually be a Ctrl+Shift+[n] type of shortcut).

Workbook_BeforePrint macro:
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim oWs As Worksheet
    
    Set oWs = ThisWorkbook.ActiveSheet
    Application.DisplayAlerts = False
    
    On Error Resume Next
    Sheets("Backup").Delete
    On Error GoTo 0
    
    oWs.Copy After:=Sheets.Count
    ActiveSheet.Name = "Backup"
    oWs.Activate
    
    Application.DisplayAlerts = True

    Set oWs = Nothing
End Sub

Undo Changes macro:
Code:
Sub UndoSheetChanges()
    Dim strSheetName As String
    Dim x As Integer
    
    strSheetName = ActiveSheet.Name
    x = ActiveSheet.Index - 1
    
    On Error GoTo NoBackup
    Sheets("Backup").Copy After:=x
    On Error GoTo 0
    
    Sheets(strSheetName).Activate
    
    Application.DisplayAlerts = False
    
    ActiveSheet.Delete
    Sheets("Backup (2)").Name = strSheetName
    
NoBackup:
    Application.DisplayAlerts = True
End Sub
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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