To close all open workbooks in Excel using VBA, you can use the Workbooks.Close method, optionally with the SaveChanges parameter to control whether prompts appear or changes are saved automatically. This saves time when you need to shut down multiple workbooks programmatically—especially useful in macros that run unattended or clean up after processing data.
What You'll Need
- Microsoft Excel installed (Excel 2010 or later recommended; VBA is included in most retail and volume-licensed versions)
- A workbook with VBA enabled (ensure the Developer tab is visible or open the VBA editor directly)
- Basic understanding of the VBA editor and macro security settings (must allow macros to run)
Step-by-Step
- Open Excel and press Alt + F11 to launch the Visual Basic for Applications (VBA) editor. If you don’t see the VBA editor window, ensure Excel is fully loaded and try again—sometimes pressing the keys twice helps if Excel is slow to respond.
- In the VBA editor, go to Insert > Module to create a new standard module. This opens a blank code window where you’ll paste the macro. Do *not* use a worksheet or ThisWorkbook module unless specifically needed.
- Copy and paste the following code into the module:
Sub CloseAllWorkbooks() Dim wb As Workbook For Each wb In Application.Workbooks wb.Close SaveChanges:=False Next wb End SubThis loop iterates through every open workbook in the current Excel instance and closes it without saving changes. If you want to save changes, replaceSaveChanges:=FalsewithSaveChanges:=True. - Press Alt + F8 to open the Macro dialog box. Confirm the macro name
CloseAllWorkbooksappears in the list. Select it, then click Run. Excel will close all open workbooks immediately—no confirmation prompts will appear if you usedSaveChanges:=FalseorTrue. - To test safely, open several sample workbooks first. Add a small change to one (e.g., type a letter in a cell), then run the macro. If you used
SaveChanges:=False, the change will be discarded; if you usedSaveChanges:=True, Excel will save the change silently and close the workbook.
Common Mistakes
- Running the macro in the wrong Excel instance: If you have multiple Excel windows open (e.g., one for each workbook), VBA only controls the instance where the macro resides.
Application.Workbooksrefers to workbooks in the *active* Excel process—not all Excel windows on your system. To close workbooks across all instances, you’d need separate macros in each instance or automation via external scripting (e.g., PowerShell), which is beyond basic VBA. - Using
ThisWorkbookinstead ofApplication.Workbooks:ThisWorkbookrefers only to the workbook containing the macro. UsingFor Each wb In ThisWorkbookwould cause an error becauseThisWorkbookis a single object, not a collection. Always useApplication.Workbooksto target all open workbooks. - Forgetting to handle unsaved workbooks: If you omit the
SaveChangesargument (e.g., justwb.Close), Excel displays a save prompt for each workbook with unsaved changes. In unattended macros, this causes the script to hang waiting for input. Always specifySaveChangesexplicitly. - Macro security blocking execution: If macro security is set to “Disable all macros without notification,” the macro won’t run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select “Enable all macros” (for testing only) or “Disable all macros with notification” to enable running via the ribbon.
Tips
- Add error handling: Wrap the loop in an
On Error Resume Nextblock if you suspect a workbook might be corrupted or read-only, then reset withOn Error GoTo 0afterward. For example:Sub CloseAllWorkbooksSafe() Dim wb As Workbook On Error Resume Next For Each wb In Application.Workbooks If Not wb Is Nothing Then wb.Close SaveChanges:=False Next wb On Error GoTo 0 End Sub - Save specific workbooks first: If you need to save one workbook but not others, explicitly close it first:
Sub CloseWithSave() ThisWorkbook.Close SaveChanges:=True 'Save and close the macro's workbook Dim wb As Workbook For Each wb In Application.Workbooks If Not wb Is ThisWorkbook Then wb.Close SaveChanges:=False Next wb End Sub - Use a confirmation prompt (optional): For safety during testing, add a message box:
Sub CloseAllWithPrompt() If MsgBox("Close all workbooks without saving?", vbYesNo + vbExclamation) = vbYes Then Dim wb As Workbook For Each wb In Application.Workbooks wb.Close SaveChanges:=False Next wb End If End Sub - Test on non-critical files: Always try the macro on dummy files first. Once confirmed, apply it to real data.
Troubleshooting
If the macro fails or doesn’t close all workbooks, follow these checks:
- Verify the correct Excel instance: Close all Excel windows, then reopen just one workbook and launch the VBA editor from there. Run the macro. If it works, the issue is instance isolation—open other workbooks *within the same Excel window* (via File > Open, not double-clicking files) to keep them in one instance.
- Check for protected workbooks: If a workbook is protected (e.g., shared with track changes), closing might fail. Unprotect it first, or use
On Error Resume Nextas shown in the tips section. - Ensure no modal dialogs are open: If a dialog (e.g., Find & Select, or an add-in popup) is blocking Excel, the macro may pause. Close all dialogs manually before running.
- Restart Excel and try again: Sometimes Excel’s VBA engine gets stuck. Save work, close Excel completely, reopen, and re-run the macro.
- Check for add-ins interfering: Disable add-ins via File > Options > Add-ins, then manage COM add-ins and go-button items. Re-enable them one by one after testing.
Frequently Asked Questions
Does Workbooks.Close close the workbook containing the macro?
Yes—if the macro is in a workbook that is open, it will be closed too. To protect the macro workbook, use ThisWorkbook.Close separately with a conditional, or skip it in the loop with If wb.Name <> ThisWorkbook.Name Then ....
Can I close only specific workbooks (e.g., all except the macro file)?
Yes. Modify the loop to check the workbook name:For Each wb In Application.Workbooks
If wb.Name <> "MyMainFile.xlsm" Then wb.Close SaveChanges:=False
Next wb
Why does Excel ask to save changes even when I set SaveChanges:=False?
It shouldn’t—if you see prompts, ensure your code *actually* uses wb.Close SaveChanges:=False (not wb.Close alone) and that you ran the updated macro. Also, verify you’re editing the correct module and saved the workbook as macro-enabled (.xlsm).
Will this close workbooks in other Excel windows?
No. Each Excel window runs in its own process. The macro only affects workbooks open in the *same* Excel instance where the macro resides.Can I undo the close action?
No. Once closed, workbooks are gone unless you have AutoRecover enabled or manually saved backups. Always test first.Does this work in Excel Online or Excel for Mac?
No—VBA macros are not supported in Excel Online. On Excel for Mac (2016 and later), VBA works similarly, but some object models differ slightly;Workbooks.Close functions the same, but verify macro security settings in Preferences > Security > Macro Security.