VBA – Force a Workbook to Save Before Close


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.