An Excel Macro to Close All Workbooks at Once
One of the more annoying things in Excel is closing many workbooks at once. For each workbook you’ve opened, you need to activate the work, close it, and confirm the saving of changes. Excel has no easy way to close them all at once. This little macro takes care of that annoyance.
How the macro works
In this macro, the Workbooks collection loops through all opened workbooks. As the macro loops through each workbook, it saves and closes them down:
Sub Macro1() 'Step 1: Declare your variables Dim wb As Workbook 'Step 2: Loop through workbooks, save and close For Each wb In Workbooks wb.Close SaveChanges:=True Next wb End Sub
Step 1 declares an Object variable that represents a Workbook object. This allows you to enumerate through all the open workbooks, capturing their names as you go.
Step 2 simply loops through the open workbooks, saving and closing them. If you don’t want to save them, change the SaveChanges argument from True to False.
How to use the macro
The best place to store this macro is in your personal macro workbook. This way, the macro is always available to you. The personal macro workbook is loaded whenever you start Excel. In the VBE project window, it is named personal.xlsb.
Activate Visual Basic Editor by pressing Alt+F11.
Right-click personal.xlb in the project window.
Type or paste the code in the newly created module.
If you don’t see personal.xlb in your project window, it doesn’t exist yet. You’ll have record a macro using personal macro workbook as the destination.
To record the macro in your personal macro workbook, open the Record Macro dialog box. In the Store Macro In drop-down list, select Personal Macro Workbook. Then simply record a few cell clicks and stop recording. You can discard the recorded macro and replace it with this one.