VBA to Prevent Saving Based on Date

Alm5423

New Member
Joined
Sep 22, 2021
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hey Folks!

I have a spreadsheet where there is date entered in cell F1. I'd like to prevent users from being able to save the spreadsheet if the date entered in cell F1 is prior to today's date.

I imagine there is a simple way to do this with VBA, I'm just not sure what the correct syntax is for date functions. Anyone have a code that would allow me to do this?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This will not work if the user opens the workbook with macros disabled, but...

Press Alt+F11 to go to the VB editor. Find the workbook in the Project Explorer pane and expand it by clicking on the "+". Open the ThisWorkbook item in the list. The ThisWorkbook code module opens. In the left dropdown at the top of the Window, select Workbook, and in the right workbook, select BeforeSave. You will see this procedure stub:

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

End Sub

Add a line of code like this to cancel the save:

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If ActiveSheet.Range("F1") < Int(Now) then
        Cancel = True
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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