Here's a well-structured VBA macro that fulfills your request. It checks the last modified date of the current workbook. If the file wasn't modified today, it prompts the user to confirm whether they want to proceed. If the user chooses No, the macro exits gracefully. If Yes, it continues with any additional code you might want to add.
✅ VBA Code
Sub CheckFileModifiedDate()
Dim lastModDate As Date
Dim response As VbMsgBoxResult
On Error GoTo ErrorHandler
' Get the last modified date of the current workbook
lastModDate = FileDateTime(ThisWorkbook.FullName)
' Compare the date part of the last modified date to today's date
If DateValue(lastModDate) <> Date Then
' Prompt user for confirmation
response = MsgBox("The file was last modified on " & Format(lastModDate, "yyyy-mm-dd") & "." & vbCrLf & _
"Do you want to proceed with the operation?", _
vbYesNo + vbExclamation, _
"Proceed Confirmation")
' If user clicks No, exit the subroutine
If response = vbNo Then
MsgBox "Operation cancelled by user.", vbInformation, "Exiting"
Exit Sub
End If
End If
' Continue with the rest of your code here
MsgBox "Proceeding with the operation...", vbInformation, "Continue"
' Additional code goes here
' ...
Exit Sub
ErrorHandler:
' Handle error if the workbook hasn't been saved yet
If Err.Number = 53 Then
MsgBox "The workbook has not been saved yet. Please save the file first.", vbCritical, "Error"
Else
MsgBox "An unexpected error occurred: " & Err.Description, vbCritical, "Error"
End If
Exit Sub
End Sub
📝 Notes
- FileDateTime retrieves the last modified date and time of the file.
- DateValue ensures only the date part is compared to today's date (
Date).
- The message box includes the exact last modified date for clarity.
- The code includes basic error handling to catch if the file has never been saved (which would cause
ThisWorkbook.FullName to be empty and trigger an error).
- You can replace the final
MsgBox with any other logic or macro steps you want to execute after confirming the file is up to date.
🔁 Customization
If you'd like to check a different file than the current workbook, replace:
lastModDate = FileDateTime(ThisWorkbook.FullName)
with:
Dim filePath As String
filePath = "C:\Path\To\Your\File.xlsx"
lastModDate = FileDateTime(filePath)
Make sure the file path is valid and accessible to avoid errors.
This macro is safe, user-friendly, and robust for most common scenarios involving file modification date checks in Excel VBA.