Dialog box is appearing twice before saving

LFKim2018

Active Member
Joined
Mar 24, 2018
Messages
267
I have written the SUB below - when I clicked [X] it is working all right - but the dialog box is appearing twice before saving. When I used the SAVE Icon - it does the same thing BUT there's a prompt that say "Excel has stop working" - when I ignore the prompt - it saves the file.

I am trying to save the file with a designated name.
Is this procedure even correct at all?
many many thanks



Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim file_name As Variant
    Dim FName As String
    On Error Resume Next
    Application.DisplayAlerts = False
    nyr = Format(Sheets("FS").Range("A2"), "yyyy")
    nfty = " for the year " & nyr & ".xlsm"
    FName = Replace(ThisWorkbook.FullName, ".xlsm", "") & nfty
    If ExactWordInString(FName, " for the year ") <> 0 Then
        FName = Replace(ThisWorkbook.FullName, ".xlsm", "")
    End If
    file_name = Application.GetSaveAsFilename(FName, _
                FileFilter:="Excel Files,*.xlsm,All Files,*.*", _
                Title:="Save As File Name")
    If file_name = False Then
        Cancel = True
    Else
        If LCase$(Right$(file_name, 5)) <> ".xlsm" Then
            file_name = file_name & ".xlsm"
        End If
        ActiveWorkbook.SaveAs Filename:=file_name
    End If
    On Error GoTo 0
    Application.DisplayAlerts = True
    Application.Quit
End Sub

Function ExactWordInString(Text As String, Word As String) As Boolean
    ExactWordInString = " " & UCase(Text) & " " Like "*[!A-Z]" & UCase(Word) & "[!A-Z]*"
End Function
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim file_name As Variant
    Dim FName As String
    On Error Resume Next
    Application.DisplayAlerts = False
    nyr = Format(Sheets("FS").Range("A2"), "yyyy")
    nfty = " for the year " & nyr & ".xlsm"
    FName = Replace(ThisWorkbook.FullName, ".xlsm", "") & nfty
    If ExactWordInString(FName, " for the year ") <> 0 Then
        FName = Replace(ThisWorkbook.FullName, ".xlsm", "")
    End If
    file_name = Application.GetSaveAsFilename(FName, _
                FileFilter:="Excel Files,*.xlsm,All Files,*.*", _
                Title:="Save As File Name")
    If file_name = False Then
        Cancel = True
    Else
        If LCase$(Right$(file_name, 5)) <> ".xlsm" Then
            file_name = file_name & ".xlsm"
        End If
        [B][COLOR="#FF0000"]Application.EnableEvents = False[/COLOR][/B]
        [B][COLOR="#0000FF"]ActiveWorkbook.SaveAs Filename:=file_name[/COLOR][/B]
        [B][COLOR="#FF0000"]Application.EnableEvents = True[/COLOR][/B]
    End If
    On Error GoTo 0
    Application.DisplayAlerts = True
    Application.Quit
End Sub

Function ExactWordInString(Text As String, Word As String) As Boolean
    ExactWordInString = " " & UCase(Text) & " " Like "*[!A-Z]" & UCase(Word) & "[!A-Z]*"
End Function
Untested, but I think the line of code I highlighted in blue above is kicking off the BeforeSave event again. Try adding what I show in red above and see if that stops the double dialog.
 
Last edited:
Upvote 0
Untested, but I think the line of code I highlighted in blue above is kicking off the BeforeSave event again. Try adding what I show in red above and see if that stops the double dialog.

Mr. Rothstein
You nailed it!!
Now the dialog box appears only once then saves the file.
clicking [X] is now OK. but with SAVE icon - the prompt "Excel has stopped working" still appears.
and when I ignore the prompt, it goes on to save the file.
What causes to prompt "Excel stopped working"?
many many thanks
 
Upvote 0
Mr. Rothstein
You nailed it!!
Now the dialog box appears only once then saves the file.
clicking [X] is now OK. but with SAVE icon - the prompt "Excel has stopped working" still appears.
and when I ignore the prompt, it goes on to save the file.
What causes to prompt "Excel stopped working"?
many many thanks

HELP, anybody?
many thanks
 
Upvote 0
Upvote 0
Upvote 0
The first half of my OP was solved by Mr. Rothstein, the other half - pertaining to the "Excel stopped working" prompt is still up in the air, if it has no solution - anybody , somebody - pls let me know, so we can close this query....
many many thanks
 
Upvote 0
Mr. Rothstein
sorry to bother you.
Thank you for solving the first half of my OP.
could you pls analyze why the prompt "Excel Stopped working" keeps appearing whenever the SAVE ICON is pressed
(although when ignored - it goes on to save the file anyway) -- though everything is alright when [X] is used.
many many thanks
 
Upvote 0
Mr. Rothstein
sorry to bother you.
Thank you for solving the first half of my OP.
could you pls analyze why the prompt "Excel Stopped working" keeps appearing whenever the SAVE ICONis pressed
(although when ignored - it goes on to save the file anyway) -- though everything is alright when [X] is used.
many many thanks
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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