une


I have a Sub in which there is an If statement. If a certain condition is found to be false in this If statement, I want Excel to display a message and then have Excel close down immediately. Here is the code;

If validName = False Then
MsgBox ("Sorry, I do not know you. Please complete a voucher manually.")
Application.DisplayAlerts = False
Application.Quit
MsgBox ("Test for immediate shut down")
End If

Excel did not shut down straight away when Application.Quit was encountered, the test messge was displayed and other code later in the Sub also executed. Why did it not cease execution immediatley upon executing Application.Quit

Further to this, if the Sub completes successfully and then the user wants to quit Excel by clicking the X at top right of the Excel application window, how do I stop the "Do you want to save changes" window from being displayed before Excel shuts down




Re: Quit excel

Peter Mo.


Hi

I don't know why the code continues after the Quit, but to stop the "Do you want to save changes" you just need to either save the workbook(s) or mark them as saved e.g.

Code Snippet
For Each w In Application.Workbooks
    w.Saved = True
Next w

Regards
Peter Mo.





Re: Quit excel

magicalclick

I do this way since I don't want to close other workbooks most of the time.

Code Snippet

' Close the workbook without saving. Close Application if no more workbooks in the application.
' Use ActiveWorkbook if no book name supplied.
Sub CloseWorkBook(Optional book As String)
If book = "" Then book = ActiveWorkbook.name
Workbooks(book).Saved = True
If Application.Workbooks.Count < 2 Then
Application.Quit
Else
Workbooks(book).Close
End If
End Sub