Finance

Charts

Statistics

Macros

Search

Automatically Saving a Workbook Before Closing (Without Prompt)

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:

  1. Place the code in the ThisWorkbook module of Mappe2.xlsm.
  2. Open the file and make a visible change (e.g., edit a cell).
  3. Close the workbook.
  4. 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.

 

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx