I wonder if anyone could explain why the following code reaches the message box "Why am I here?" and Application.Quit doesn't do what it says? When this code is called by hitting the top right X, at least.
(By the way - why am I using wb as Window instead of as Workbook? Because some windows are hidden, so don't appear in the Workbooks list.)
This code is in ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wb As Window
Dim username As String
Application.EnableEvents = False
username = LCase(Environ("USERNAME"))
For Each wb In Application.Windows
' automatically save any audit sheets
If Left(wb.Caption, Len(username)) = username Then
Workbooks(wb.Caption).Save
End If
' just close anything else (use system to ask if save changes)
' except this sheet itself (else the loop stops)
If wb.Caption <> ThisWorkbook.Name Then
Workbooks(wb.Caption).Close
End If
Next wb
' and finally close this (last remaining open) workbook
Application.Quit
MsgBox "Why am I here?"
End Sub