troubleshooting Question

Excel VBA Before_Save code not generating desired outcome when user aborts message box prompt

Avatar of doctornick0
doctornick0Flag for United States of America asked on
VBAMicrosoft ExcelMicrosoft Office
3 Comments1 Solution104 ViewsLast Modified:
Hi, everybody,

I have an Excel VBA Before Save procedure to check if the "as of" date listed in cell A2 on the front page of the Excel report has been updated (code listed below).  If the cell contains a date, and the date differs from the current date by more than 1 day, it prompts the user via message box to ask if the user wants to change the date first.  If the user selects yes, it takes them to the cell that contains the date, if they select no, it is intended to open the Save dialog box.

All of the above pieces work unless the user selects "No" to that message box.  In that case, the message box comes back up 2 additional times, and then the "User cancelled" message box comes up, though the Save File Dialog finally does appear after this.  I am not sure why this occurs, but I am looking to correct this, so that if the user selects "no" to the message box, only the File Save As dialog appears.

The macro is listed below:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Purpose:  Check before saving the file if the "as of" date is up to date
Dim ws As Worksheet

'Check cell A2 on worksheet "FrontPage_RevTeam"
Set ws = Sheets("FrontPage_RevTeam")
Dim DateCheck, CheckBeforeSave
'Check that cell A2 has a value in it, then check if that value is a date
DateCheck = ws.Range("A2").Value
If Not IsEmpty(DateCheck) Then
    If IsDate(DateCheck) Then
    'Check if the workbook has any changes made to it
        If ActiveWorkbook.Saved = False Then
            'Check if the value in A2 is older than today
            If DateDiff("d", DateCheck, Date) > 1 Then
                'Trigger a message box to the user to ask if they wanted to update the date
                CheckBeforeSave = MsgBox("Did you want to update the date?", vbYesNo + vbQuestion, "DateCheck")
                    'If yes, then set the cursor into FrontPageRev_Team!A2
                    If CheckBeforeSave = vbYes Then
                        ws.Range("A2").Activate
                    Else
                    'If no, trigger the File Save As dialog box
                        Dim bFileSaveAs As Boolean
                        bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
                        If Not bFileSaveAs Then MsgBox "User cancelled", vbCritical
                    End If
            End If
        End If
    End If
End If

End Sub

Thanks for any help you can provide,

doctornick0
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros