Changing the behavior of ctrl+save in Excel

Pardeep Singh

New Member
Joined
Feb 8, 2023
Messages
10
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
I am trying to write a vba code to mutate the behavior of excel when user presses ctrl+s. I am updating the workbook so that when a user presses the ctrl+s key combo, a vba code is triggered. This will save the file with new name (save as). But the condition is the current execution of code should be 2 hours after the previous execution else it should just normally save the workbook.

Wrote following code to achieve it, but cannot connect it to keyboard shortcut ctrl+s

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim NewFileName As String
    Dim LastExecTime As Date
    
    'Get the last execution time from a named range called "LastExecTime"
    On Error Resume Next
    LastExecTime = Range("LastExecTime").Value
    If Err.Number <> 0 Then LastExecTime = Now - TimeSerial(2, 0, 0) 'Use current time - 2 hours as default value
    On Error GoTo 0
    
    'Check if the time difference between the last execution and the current time is at least 2 hours
    If DateDiff("h", LastExecTime, Now) >= 2 Then
        NewFileName = "V1_" & ThisWorkbook.Name 'Create the new file name with "V1_" as prefix and the current workbook name
        
        Application.DisplayAlerts = False 'Disable alerts to prevent the "Save As" dialog box from appearing
        ThisWorkbook.SaveAs Filename:=NewFileName, FileFormat:=xlOpenXMLWorkbook 'Save the workbook with the new file name
        Application.DisplayAlerts = True 'Re-enable alerts
        
        Cancel = True 'Cancel the default save function
        
        'Update the last execution time in the named range "LastExecTime"
        On Error Resume Next
        Range("LastExecTime").Value = Now
        If Err.Number <> 0 Then
            With Worksheets.Add
                .Name = "Settings"
                .Range("A1").Value = "LastExecTime"
                .Range("B1").Value = Now
                .Range("A1:B1").Name = "LastExecTime"
                .Visible = xlVeryHidden
            End With
        End If
        On Error GoTo 0
    Else
        Cancel = False 'Allow the default save function to execute
    End If
End Sub


Can someone please help me out with what am I missing.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
It should simply work, regardless of how you are saving.

Just to make sure: This code you have is code in the 'ThisWorkbook' module. Not in a normal module.

Start the code with
VBA Code:
Debug.Print "Saved"

and comment out the rest.
Then do a save with Ctrl-S
Go back to VBA and it should show 'Saved' in the immediate window (bottom left).

If that works, then I would suggest to step through the code (by pressing the F8 key repeatedly) to see why it is not behaving as you expect. See more on how to debug in the document link in my tagline below.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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