To make Excel automatically save a workbook before it closes, you must use a VBA macro placed specifically within the "ThisWorkbook" object using the Workbook_BeforeClose event. This ensures that every time the user attempts to exit the file, the system triggers a save command before the application terminates the session, preventing accidental data loss.

What You'll Need

  • Microsoft Excel installed on a Windows or macOS computer.
  • A workbook that you have permission to edit and save.
  • The Developer tab enabled in the Excel Ribbon (though you can also use the Alt + F11 shortcut).
  • Basic familiarity with navigating the Visual Basic for Applications (VBA) editor.

Step-by-Step

  1. Open the VBA Editor: Open the Excel workbook you wish to automate. Press Alt + F11 on your keyboard. This is the universal shortcut to open the Visual Basic for Applications editor window regardless of whether the Developer tab is visible.
  2. Locate the ThisWorkbook Object: On the left-hand side of the screen, you will see the Project Explorer pane. If you do not see it, press Ctrl + R. Look for the folder named Microsoft Excel Objects. Inside this folder, double-click on the item labeled ThisWorkbook. This is critical; the code will not work if it is placed in a standard Module (e.g., Module1). It must be in the workbook-level object to detect the close event.
  3. Select the Event Procedure: At the top of the code window for ThisWorkbook, you will see two dropdown menus. The left dropdown currently says "(General)". Click it and select Workbook. Once you do this, the right dropdown will automatically populate with a list of events. Click the right dropdown and select BeforeClose.
  4. Insert the Save Command: Excel will automatically generate the shell of the subroutine: Private Sub Workbook_BeforeClose(Cancel As Boolean) and End Sub. Inside these two lines, type the following command on its own line:
    ThisWorkbook.Save
    The complete block of code should look exactly like this:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        ThisWorkbook.Save
    End Sub
  5. Test the Macro: Close the VBA editor window by clicking the X in the top right or pressing Alt + Q. Make a small change to a cell in your spreadsheet. Now, click the close button (X) of the Excel window. You should notice that Excel closes immediately without prompting you to save changes, because the macro has already executed the save command in the background.
  6. Save as a Macro-Enabled Workbook: This is the most important step for persistence. Standard .xlsx files cannot store VBA code. Go to File > Save As. In the file type dropdown menu, select Excel Macro-Enabled Workbook (*.xlsm). If you save it as a standard workbook, all your code will be stripped out and the auto-save will stop working the next time you open the file.

Common Mistakes

The most frequent error is placing the code in a standard module. Many users go to Insert > Module and paste the code there. However, a standard module is for general procedures that are called manually or by other macros. Event-driven code—code that reacts to an action like closing, opening, or changing a cell—must reside in the object it is monitoring. For this specific task, that is always ThisWorkbook.

Another common mistake is forgetting to change the file extension to .xlsm. Users often spend an hour troubleshooting why the macro worked once but disappeared after the first restart. Because .xlsx files are designed to be XML-based and macro-free for security reasons, Excel will warn you when saving that the project will be lost; ignoring this warning results in the deletion of the auto-save script.

Some users attempt to use ActiveWorkbook.Save instead of ThisWorkbook.Save. While this often works, ThisWorkbook is more precise because it refers specifically to the workbook where the code is stored, whereas ActiveWorkbook refers to whatever window happens to be on top. If a user has multiple workbooks open, ActiveWorkbook could potentially save the wrong file if the focus shifts at the millisecond the close command is triggered.

Tips

If you want to avoid the "Save" prompt entirely and ensure the user never sees it, you can combine the save command with a property that tells Excel the workbook is already saved. Add ThisWorkbook.Saved = True after the save command. This is particularly useful in complex environments where other add-ins might trigger a save prompt even after the macro has run.

For those working in shared environments or on network drives, consider adding a simple error handler. If the file is marked as "Read Only" or if the network connection drops, the ThisWorkbook.Save command will trigger a runtime error and crash the macro. You can prevent this by adding On Error Resume Next at the top of the subroutine. This tells Excel to ignore the error and proceed with closing the file if the save fails, rather than showing a debug window to the end-user.

If you want to save the file to a specific backup location every time it closes (creating a version history), you can use ThisWorkbook.SaveCopyAs "C:\Backups\MyWorkbook_Backup.xlsm". This saves a copy of the file to a different path without changing the name or location of the primary file the user is currently working in.

Troubleshooting

If the workbook still asks you to save changes when closing, first check if macros are enabled. Look for a yellow "Security Warning" bar at the top of the spreadsheet that says "Macros have been disabled." Click Enable Content. If this bar does not appear, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure that macros are enabled (preferably using the "Disable all macros with notification" setting so you can choose when to enable them).

If the code is present but nothing happens, verify the location of the code. Open the VBA editor (Alt + F11) and ensure the code is inside the ThisWorkbook object and not in Sheet1 or Module1. The event name must be exactly Workbook_BeforeClose; if there is a typo in the name, Excel will treat it as a regular subroutine and will not trigger it automatically during the close sequence.

If you receive a "Runtime Error 1004" when closing, it usually means the file is read-only or the destination folder is restricted. Check the file properties in Windows Explorer to ensure the "Read-only" attribute is unchecked. If the file is on a shared drive, ensure you have "Write" permissions for that specific folder.

Frequently Asked Questions

Will this work if I close Excel entirely instead of just the workbook?

Yes. The Workbook_BeforeClose event triggers whenever the specific workbook is closed, regardless of whether you close the individual window or exit the entire Excel application.

Can I use this to auto-save every 5 minutes instead?

No, the BeforeClose event only triggers upon exiting. To save every few minutes, you would need to use the Application.OnTime method to create a recurring timer macro that runs in the background.

Does this work on Excel for the Web?

No. VBA (Visual Basic for Applications) is only supported in the desktop versions of Excel for Windows and macOS. Excel for the Web uses Office Scripts (TypeScript), which has a different architecture and does not support the same event-driven triggers as VBA.

Will this save the file even if I click "Don't Save"?

Yes. Because the ThisWorkbook.Save command executes before Excel asks the user if they want to save, the file is already saved by the time the prompt would normally appear. In most cases, the prompt will not even appear because Excel sees the file as already saved.

Can I make it save to a different filename automatically?

Yes, by using ThisWorkbook.SaveAs instead of ThisWorkbook.Save. However, be careful: SaveAs changes the active file to the new name, meaning the user will be working in the new file rather than the original one.