To save all open workbooks in Excel using VBA, you must create a macro that loops through the Workbooks collection and executes the .Save method for each item. This is particularly useful for users who work with multiple data sources simultaneously and want to avoid clicking "Save" on every individual window before closing the application.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- The Developer tab enabled in the Excel Ribbon.
- Workbooks that have been saved at least once (new, unsaved workbooks require a filename and path, which a simple .Save command cannot provide).
Step-by-Step
- Enable the Developer Tab: If you do not see the Developer tab at the top of your screen, right-click any existing tab on the Ribbon, select Customize the Ribbon, and in the right-hand list, check the box next to Developer. Click OK. (Note: Menu labels may differ slightly depending on your version of Excel).
- Open the VBA Editor: Click the Developer tab and select the Visual Basic button, or press
Alt + F11on your keyboard. - Insert a New Module: In the VBA Editor, go to the top menu and select Insert > Module. This creates a blank space where your code can live independently of a specific sheet.
- Enter the Code: Copy and paste the following code into the module window:
Sub SaveAllWorkbooks() Dim wb As Workbook ' Loop through every workbook currently open in the Excel instance For Each wb In Application.Workbooks ' Save the workbook wb.Save Next wb MsgBox "All open workbooks have been saved successfully!", vbInformation End Sub - Save the Macro-Enabled Workbook: Because standard .xlsx files cannot store macros, you must save the workbook containing this code as an Excel Macro-Enabled Workbook (.xlsm). Go to File > Save As and select the .xlsm format from the dropdown menu.
- Run the Macro: You can run the macro by pressing
F5while inside the VBA editor, or by returning to the Excel sheet, clicking the Developer tab, selecting Macros, choosing "SaveAllWorkbooks" from the list, and clicking Run.
Common Mistakes
- Trying to save "Book1": The
.Savemethod only works on files that already have a saved location on your hard drive or cloud. If you have a brand new workbook that has never been saved, the macro will trigger a "Save As" dialog box, interrupting the automation. - Saving as .xlsx: If you save the file containing the macro as a standard workbook (.xlsx), the VBA code will be stripped out and deleted when you close the file. Always use .xlsm.
- Ignoring Read-Only files: If one of the open workbooks is marked as "Read-Only," the macro may pause or throw an error because it cannot overwrite the existing file.
Tips
- Create a Shortcut Button: Instead of opening the Macro menu every time, go to the Developer tab, click Insert, and choose the Button (Form Control). Draw the button on your sheet and assign it to the "SaveAllWorkbooks" macro for one-click saving.
- Add to Quick Access Toolbar: You can add the macro to the Quick Access Toolbar (the small icons at the very top of the Excel window) by going to File > Options > Quick Access Toolbar, selecting "Macros" from the dropdown, and adding your script.
- Combine with Close: If you want to save and then immediately close everything, you can add
wb.Closeimmediately afterwb.Savein the code loop.
Troubleshooting
If the macro does not run as expected, try these steps in order:
- Check Macro Settings: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure that "Disable VBA macros with notification" is selected. If macros are completely disabled, the code will not execute.
- Verify Workbook State: Ensure no cells are currently in "Edit Mode" (where the cursor is blinking inside a cell). VBA cannot run while you are actively typing in a cell.
- Check for Dialog Boxes: If another workbook has an open alert or a "Find and Replace" window active, the macro will be blocked until that window is closed.
- Restart Excel: If the VBA editor becomes unresponsive, save your work manually and restart the application to clear the memory cache.
Frequently Asked Questions
Will this save workbooks that have never been saved before?
No. The .Save command updates an existing file. For new workbooks, Excel will prompt you to choose a folder and filename via a popup window.
Can I use this to save workbooks open in different Excel instances?
No. This macro only affects workbooks open within the same "instance" of the Excel application. If you have launched Excel twice from the Start menu, the macro in one instance cannot see the workbooks in the other.
Is there a way to save all workbooks without the confirmation message?
Yes. If you remove the MsgBox line from the code, the macro will run silently in the background without any popup notification.
Does this work on Excel for Mac?
Yes, the VBA code for saving workbooks is largely the same across Windows and macOS, provided you have the Developer tab enabled.
Will this save files stored on OneDrive or SharePoint?
Yes, as long as the files are already synced and have a designated path, the .Save method will trigger the AutoSave or manual save process for cloud-hosted files.