The following VBA procedure ensures that a workbook is automatically saved when the user attempts to close it — without Excel asking whether to save changes.
Example Code: Workbook_BeforeClose Event
Private Sub Workbook_BeforeClose(Cancel As Boolean) ThisWorkbook.Save End Sub
Explanation:
What is Workbook_BeforeClose()?
- Workbook_BeforeClose is an event procedure that executes just before the workbook is closed.
- It must be placed in the ThisWorkbook module, not in a standard module.
- The Cancel parameter allows you to interrupt the closing process (e.g., based on a condition), but it’s unused here.
What This Procedure Does:
- Calls:
- Save
This saves the workbook immediately without displaying the default “Do you want to save changes?” dialog box.
- Because this occurs before Excel prompts the user, the normal save confirmation is skipped — making the process fully automatic.
Behavior Without This Procedure:
Normally, when a user makes changes and tries to close the workbook:
- Excel displays a confirmation dialog:
“Do you want to save the changes you made…?”
With this macro in place:
- The workbook is saved silently, and the close operation continues without interruption.
How to Test It:
- Place the code in the ThisWorkbook module of Mappe2.xlsm.
- Open the file and make a visible change (e.g., edit a cell).
- Close the workbook.
- No prompt will appear — the file is saved automatically and closes.
Summary
| Feature | Details |
| Event name | Workbook_BeforeClose |
| Module | Must be in ThisWorkbook |
| Purpose | Save workbook automatically on close |
| Prevents save prompt | Yes |
| Parameter | Cancel As Boolean (optional — can be used to cancel close) |
| Save method used | ThisWorkbook.Save |
If needed, you can enhance this event to include conditions (e.g., only save if specific cells are changed) or to log the save operation.