by Allen Wyatt
(last updated June 22, 2019)
Gary has a read-only workbook that multiple users can access. They can modify cells but not save their work. On exiting the workbook, Gary wants Excel to just close without informing the user that it is read-only and giving them the option of saving a copy.
This is best accomplished by using a macro to modify the Saved flag in the workbook, just before closing. This flag indicates, internally, whether a workbook needs saving or not. If the flag is False, then Excel knows that the workbook has not been saved (changes have been made without saving). If your macro sets the flag to True, then Excel will close directly because it thinks that all the changes have been saved.
Here's what the macro should look like, at its simplest:
Private Sub Workbook_BeforeClose(Cancel As Boolean) If ThisWorkbook.ReadOnly Then ThisWorkbook.Saved = True End If End Sub
The macro should be added to the ThisWorkbook object in the VBA Editor. That way, it is automatically executed just before the workbook is closed. The flag is set to True, and when the macro ends, Excel continues with its normal closing procedures. Since Excel thinks that there are no unsaved changes, the user sees no message and the workbook is closed.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10349) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
If you have two workbooks that each have the same name, opening them at the same time in Excel could cause some problems. ...Discover More
Having trouble saving a workbook? It could have to do with the age, size, and complexity of that workbook. This tip ...Discover More
Do you need to know when a workbook was last changed? There are a couple of ways you can go about keeping track of the ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.