Closing Excel Application using VBA Closing Excel Application using VBA vba vba

Closing Excel Application using VBA


I think your problem is that it's closing the document that calls the macro before sending the command to quit the application.

Your solution in that case is to not send a command to close the workbook. Instead, you could set the "Saved" state of the workbook to true, which would circumvent any messages about closing an unsaved book. Note: this does not save the workbook; it just makes it look like it's saved.

ThisWorkbook.Saved = True

and then, right after

Application.Quit


To avoid the Save prompt message, you have to insert those lines

Application.DisplayAlerts = FalseThisWorkbook.SaveApplication.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 = FalseThisWorkbook.SaveApplication.DisplayAlerts = TrueApplication.QuitEnd Sub

Hope it help you solve the problem.


Application.Quit 

Should do the trick.