Link to home
Start Free TrialLog in
Avatar of doctornick0
doctornick0Flag for United States of America

asked on

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

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
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of byundt
byundt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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