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

doctornick0
doctornick0 used Ask the Experts™
on
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

Open in new window


Thanks for any help you can provide,

doctornick0
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I added lines 24 and 27.

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
                        Application.EnableEvents = False
                        bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
                        If Not bFileSaveAs Then MsgBox "User cancelled", vbCritical
                        Application.EnableEvents = True
                    End If
            End If
        End If
    End If
End If

Open in new window

Mechanical Engineer
Most Valuable Expert 2013
Top Expert 2013
Commented:
When I tested the suggested code, it saved the file even if I said not to. The cure, I believe is to set Cancel to True in such circumstances.
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
                        Application.EnableEvents = False
                        bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
                        If Not bFileSaveAs Then
                            MsgBox "User cancelled", vbCritical
                            Cancel = True
                        End If
                        Application.EnableEvents = True
                    End If
            End If
        End If
    End If
End If
End Sub

Open in new window

byundtMechanical Engineer
Most Valuable Expert 2013
Top Expert 2013

Commented:
I also noticed that the SaveAs dialog defaults to one of those #%*!! "copy of" style names. The cure for that is to pass the path and filename to the Dialog.Show method.
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
                        Application.EnableEvents = False
                        bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show(ThisWorkbook.FullName)
                        If Not bFileSaveAs Then
                            MsgBox "User cancelled", vbCritical
                            Cancel = True
                        End If
                        Application.EnableEvents = True
                    End If
            End If
        End If
    End If
End If
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial