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 SubStep 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.
-
Choose Insert→Module.
-
Type or paste the code in the newly created module.
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.