To close Excel from VBA, use Application.Quit. That single line shuts down the whole Excel application, not just the workbook your macro is running in. If you only want to close one workbook and leave Excel running, use ActiveWorkbook.Close or ThisWorkbook.Close instead, and save or discard changes explicitly with SaveChanges:=True or SaveChanges:=False. The tricky part is not the closing line itself but deciding whether the macro should keep running after the application that owns it has been shut down, and handling the prompts Excel throws up when there are unsaved files.
What You'll Need
- Microsoft Excel on Windows or Mac, with the Developer tab enabled (File > Options > Customize Ribbon, or Excel > Preferences > Ribbon & Toolbar on Mac — labels differ slightly by version).
- Access to the Visual Basic Editor, opened with Alt+F11 on Windows or Option+F11 on Mac.
- A saved workbook. Unsaved work is at the centre of almost every problem with closing Excel from code.
- A basic idea of whether the macro should close one workbook or the entire application. These are two different commands and behave very differently.
Step-by-Step
- Open the editor. Press Alt+F11 (Option+F11 on Mac) to open the Visual Basic Editor. In the Project window, right-click your workbook and choose Insert > Module if you want a plain macro, or double-click an existing module to add to it.
- Create the procedure. Type
Sub CloseExcelNow()on one line andEnd Subon another. Everything goes between them. - Decide what has to be saved first. Before you quit, loop through open workbooks and save the ones you want to keep. A short loop such as
For Each wb In Application.Workbookswithwb.Saveinside, closed byNext wb, handles this for you. If a workbook was never saved it has no file path, soSaveraises an error — guard it withIf Not wb.Saved Thenand skip or prompt as appropriate. - Turn off alerts. Add
Application.DisplayAlerts = Falsebefore the closing command. Without this, Excel asks "Do you want to save your changes?" for every modified workbook, and because nobody can click the dialog while a macro is running, the macro appears to freeze. - Close the application. Add
Application.Quit. This is the line that exits Excel entirely. Note that it does not necessarily end the macro instantly — the VBA engine keeps executing the remaining lines until the procedure finishes or ends. - Finish cleanly. Put any cleanup before the quit line. Once Excel has begun shutting down, statements that touch workbooks, ranges or sheets will fail. Do not add code after
Application.Quitthat expects the application to still be usable. - Restore alerts if the macro might not actually close. If your procedure can exit before quitting (for example when a condition is not met), set
Application.DisplayAlerts = Trueon that path, otherwise alerts stay suppressed for the rest of that Excel session. - Save the module and test. Press Ctrl+S in the editor, then run the macro from the Developer tab with Macros, or with F5 while the cursor is inside the procedure. Watch whether a save prompt appears and whether Excel actually disappears from the taskbar.
A working minimal version looks like this: a Sub that sets DisplayAlerts to False, calls ThisWorkbook.Save, then Application.Quit. That closes Excel and leaves no prompt. If you want to close only the current file, replace Application.Quit with ThisWorkbook.Close SaveChanges:=True and skip the alerts line entirely.
Common Mistakes
Confusing Close with Quit. Workbook.Close closes a file; Application.Quit exits Excel. People run Close expecting the window to vanish and it does not, because Excel with zero workbooks still stays open.
Leaving alerts on. This is the classic cause of a stuck macro. Excel waits for a click that never comes, and the process hangs invisibly.
Assuming code after Quit runs. Statements that manipulate objects after the quit call can trigger errors or silently do nothing. Do the cleanup first.
Forgetting to re-enable alerts. If DisplayAlerts was set to False and Excel did not shut down, the setting persists. The user keeps working with no save warnings and no overwrite prompts, which is dangerous in shared files.
Quitting from a macro stored in a workbook that is then closed. If the code lives in ThisWorkbook and you quit, the module is gone from memory. On the next run the macro must be started again from an open file.
Using Quit inside a Personal Macro Workbook routine that is supposed to finish other work. Personal.xlsb shut down means your routine stops mid-flow, often leaving other applications, Outlook items or file handles open.
Tips
- Keep a copy of the workbook open elsewhere, or use a separate add-in, when you want to shut down a second Excel instance while working in a first — otherwise you close your own session.
- Wrap the quit sequence in error handling: an
On Error GoTolabel with a cleanup block that restoresDisplayAlerts = Trueprotects users if something fails before the quit line. - Set
Application.ScreenUpdating = Falseat the start when you are closing many workbooks in a loop; it reduces flicker on slower machines. - Test with unsaved changes deliberately. Run the macro once with a modified, never-saved workbook to confirm your guards work before you release it to anyone else.
- Give the macro an obvious name such as ShutdownExcel so colleagues do not confuse it with a refresh or export routine.
Troubleshooting
Excel stays open after the macro runs. Check that you used Application.Quit and not only Workbook.Close. Also confirm the procedure reached the quit line — put a temporary MsgBox just before it during testing.
A save prompt appears and nothing happens. DisplayAlerts was not set to False, or a Workbook_BeforeSave or Workbook_BeforeClose event handler in your file is cancelling the close. Inspect the ThisWorkbook module for such handlers and remove or adjust them.
The macro errors with "Object required" or "Application-defined or object-defined error". Code is trying to touch a workbook or sheet after the quit call. Move that work above Application.Quit.
Excel closes but reopens immediately. Another open file or an add-in is starting Excel again, or a scheduled task or another Office application is automating Excel. Close other add-ins via File > Options > Add-ins and retest.
The macro quits but the VBA editor stays open. The editor is a separate window and can survive even after Excel's document windows close. Close it with Alt+Q (or Option+Q on Mac) or let the process end.
Frequently Asked Questions
What is the difference between Workbook.Close and Application.Quit?
Close shuts one workbook and leaves Excel running, possibly with no files open. Quit exits the whole Excel application and every workbook in it.
Can VBA close Excel without saving any changes?
Yes. Set Application.DisplayAlerts = False and then call Application.Quit, or close each workbook with SaveChanges:=False. Unsaved edits are discarded permanently.
Why does my macro hang when closing Excel?
Almost always a suppressed-nowhere save prompt. Excel shows a dialog that the macro cannot answer. Setting DisplayAlerts = False first fixes it.
Does Application.Quit stop my VBA code immediately?
No. The procedure can keep executing after the call, but Excel is shutting down, so avoid any further references to workbooks or ranges and end the procedure promptly.
Can I close Excel from a Personal Macro Workbook?
Yes, but it closes your entire Excel session, including Personal.xlsb, and the macro ends with it. Restart Excel to use the macro again.
How do I close a specific workbook and keep Excel open?
Use Workbooks("FileName.xlsx").Close SaveChanges:=True, or ThisWorkbook.Close if the code lives in the file you want to shut.