VBA – Force a Workbook to Save Before Close

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 19, 2021


Force Workbook to Save – Workbook_BeforeClose

I wouldn’t advise using this, but for the sake of experimentation and as a demonstration of ActiveWorkbook.Save:

You can Save a workbook without giving the user a “Yes/No” option with the following code:

Place this in the ThisWorkbook module…



Private Sub Workbook_BeforeClose(Cancel As Boolean)



If Saved = False Then

    ActiveWorkbook.Save

End If



End Sub

This doesn’t work for multiple workbooks. To make it work with multiple workbooks you’d need to place the code in the personal.xls file or have it loop through all open workbooks.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!


<<Return to VBA Examples

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples