I have used the following without success. The active workbook closes, indeed, but the excel window remains open.
Application.ActiveWindow.Close SaveChanges:
Sub button2_click()
'
' Button2_Click Macro
'
' Keyboard Shortcut: Ctrl+Shift+Q
'
ActiveSheet.Shapes("Button 2").Select
Selection.Characters.Text = "Logout"
ActiveSheet.Shapes("Button 2").Select
Selection.OnAction = "Button2_Click"
ActiveWorkbook.Saved = True
ActiveWorkbook.Save
Application.Quit
End Sub
In my case, I needed to close just one excel window and not the entire application, so, I needed to tell which exact window to close, without saving it.
The following lines work just fine:
Sub test_t()
Windows("yourfilename.xlsx").Activate
ActiveWorkbook.Close SaveChanges:=False
End Sub
Sub TestSave()
Application.Quit
ThisWorkBook.Close SaveChanges = False
End Sub
This seems to work for me, Even though looks like am quitting app before saving, but it saves...
You can try out
ThisWorkbook.Save
ThisWorkbook.Saved = True
Application.Quit
To avoid the Save prompt message, you have to insert those lines
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
After saving your work, you need to use this line to quit the Excel application
Application.Quit
Don't just simply put those line in Private Sub Workbook_Open() unless you got do a correct condition checking, else you may spoil your excel file.
For safety purpose, please create a module to run it. The following are the codes that i put:
Sub testSave()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End Sub
Hope it help you solve the problem.
I tried a certain sequence that seems to work as you can see below:
ThisWorkbook.Saved = True
Application.Quit
Application.ActiveWindow.Close SaveChanges:=False
ActiveWorkbook.Close SaveChanges:=False