Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Quit Macro: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

A VBA quit macro closes Excel cleanly from inside your own code, so you can finish a routine, save what needs saving, and shut the application down without leaving orphaned processes or prompts hanging on screen. The shortest working version is Application.Quit placed at the end of your macro, but the reliable version wraps it in a save decision and closes any open workbooks first. This guide covers that reliable version, the common ways it goes wrong, and how to test it without losing work.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Labels vary slightly by version and region.
  • A workbook saved as .xlsm (macro-enabled) so your code persists.
  • A test workbook you do not mind losing, because a quit macro can close Excel before you expect it to.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, find your workbook and right-click it, then choose Insert > Module. A blank module appears.
  2. Decide what should happen to unsaved work. Application.Quit on its own will prompt the user to save each unsaved workbook, or discard changes if alerts are suppressed. Decide whether your macro should save automatically, prompt, or discard. Never discard silently in a workbook other people use.
  3. Write the basic macro. Start with a named subroutine, for example:
    Sub QuitExcel()
        Application.Quit
    End Sub
    Run it with F5. Excel closes. If any workbook has unsaved changes, Excel shows its normal save prompt.
  4. Add a save step for your own workbook. If the macro's job is to save results and then exit, save explicitly first:
    Sub SaveAndQuit()
        ThisWorkbook.Save
        Application.Quit
    End Sub
    ThisWorkbook refers to the workbook containing the code, so it saves the right file even if others are open.
  5. Close other workbooks first if you need a clean exit. Application.Quit tries to close every open workbook. If one has unsaved changes, the user gets a prompt for each. To avoid a stack of prompts, loop through the Workbooks collection and close each one, saving or discarding as your policy requires:
    Sub CloseAllAndQuit()
        Dim wb As Workbook
        For Each wb In Application.Workbooks
            wb.Close SaveChanges:=True
        Next wb
        Application.Quit
    End Sub
    Change SaveChanges:=True to False only if you are certain nothing needs keeping.
  6. Suppress alerts only when you mean it. Setting Application.DisplayAlerts = False stops Excel's save and overwrite prompts. If you use it, set it back to True before quitting, and understand that unsaved changes will be lost without warning. For most shared workbooks, leave alerts on.
  7. Test in a disposable file. Save your work, close other important files, then run the macro. Confirm Excel closes, no hidden EXCEL.EXE process remains (check Task Manager on Windows), and your data was saved as intended.
  8. Attach the macro to a button or shortcut if needed. On the Developer tab, insert a Form Control button and assign your macro, or use Options > Customize Ribbon to add it to the Quick Access Toolbar. On Mac, right-click the toolbar and choose Customize Toolbar.

Common Mistakes

  • Quitting before saving. Application.Quit does not save for you. If alerts are off, everything since the last save is gone.
  • Leaving DisplayAlerts off. If your macro errors out after setting DisplayAlerts = False, Excel keeps suppressing prompts for the rest of the session, which confuses the next user.
  • Quitting from a workbook that is still calculating. If your macro launched a long calculation or a refresh, let it finish before calling Quit, or the file may close mid-update.
  • Assuming Quit closes only your file. It closes the whole Excel application, including every other open workbook. Warn users or close other files deliberately.
  • Running the macro from the VBA editor while stepping through code. Quit can end the debug session abruptly. Run it normally when testing the full exit path.

Tips

  • Use ThisWorkbook.Save rather than ActiveWorkbook.Save so the macro always saves the file it lives in.
  • If you want Excel to close but leave a message for the user, show a MsgBox before Application.Quit; after Quit, no code runs.
  • For scheduled or unattended runs, save explicitly and close workbooks one by one before quitting, so a single prompt cannot stall the process.
  • Keep a backup copy of any workbook before testing a quit macro that saves automatically.
  • On Mac, the VBA editor shortcuts and some dialog wording differ; the object model calls such as Application.Quit behave the same.

Troubleshooting

If Excel does not close, check whether a modal dialog is waiting behind the Excel window, such as a save prompt or a macro error box. Dismiss it and run again. If the macro stops with a runtime error, open the VBA editor and use Debug > Compile to catch syntax problems, then step through with F8 to see which line fails.

If Excel closes but a process stays in Task Manager, an add-in or a pending calculation may be holding it open. Disable add-ins temporarily and retest. If your data was not saved, confirm you called Save before Quit and that DisplayAlerts was not suppressing a failed save. If the macro quits too early, move the Quit line to the very end of the routine and check that no error handler jumps past your save step.

If the button does nothing, confirm the workbook is .xlsm, macros are enabled in the Trust Center, and the button is assigned to the correct macro name. On a locked-down machine, macro security settings may block the code entirely; that is a policy setting, not a bug in your macro.

Frequently Asked Questions

Does Application.Quit save my workbook automatically?

No. It closes Excel and relies on the normal save prompts unless alerts are suppressed. Save explicitly before calling Quit.

Can a VBA macro close Excel without any prompts?

Yes, by saving or closing each workbook with an explicit SaveChanges value and optionally setting DisplayAlerts to False. Do this only when you are sure no unsaved work matters.

Will Application.Quit close other open workbooks too?

Yes. It shuts down the whole Excel application, so every open workbook is affected. Close the ones you care about first if that is not what you want.

Why does Excel stay open after my macro runs Quit?

Usually a hidden dialog, a pending calculation, or an add-in is blocking the shutdown. Dismiss any prompt and retest with add-ins disabled.

Is there a difference between Quit and Close?

Yes. Workbook.Close closes one workbook and leaves Excel running; Application.Quit exits Excel entirely.

Can I undo a quit macro run?

No. Once Excel closes, unsaved changes are gone. Keep backups and test on a disposable file first.