Written by Allen Wyatt (last updated May 6, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
When you are developing a worksheet that will be used by other people, you may want to make sure that they fill in certain cells before they are allowed to close the workbook. There is no built-in function in Excel to do this, but you can create a macro that will make the necessary check and stop the user for proceeding. This can be a rather simple macro, tied to the BeforeClose event.
The BeforeClose event is triggered whenever a workbook is closed by whatever means. The trick is the setting of the Cancel property within the event handler. Setting Cancel to True will stop the closing of the workbook and leaving it unchanged results in the workbook closing normally.
For example, the following macro checks whether cell A1 has anything in it; if it does, then the workbook is closed. If it doesn't, then the user is informed that something is missing, and the closing is canceled.
Private Sub Workbook_BeforeClose(Cancel As Boolean) If Cells(1, 1).Value = "" Then MsgBox "Please fill cell A1" Cancel = True End If End Sub
More elaborate macros can be created, if desired. For instance, you might have several different cells that need to be checked. The following version checks a range named "Mandatory" to see if each cell in the range contains something. If any of the cells are empty, then the workbook cannot be saved or closed. (This macro is triggered not only during the BeforeClose event, but also during the BeforeSave event.) These two event handlers should be placed in the code sheet for the workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean) Cancel = ForceDataEntry() End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _ Cancel As Boolean) Cancel = ForceDataEntry() End Sub
Note that the event handlers call the ForceDataEntry function. This function should be placed in a regular macro module:
Function ForceDataEntry() As Boolean Dim rng As Range Dim c As Variant Dim rngCount As Integer Dim CellCount As Integer Set rng = Range("Mandatory") rngCount = rng.Count CellCount = 0 For Each c In rng If Len(c) > 0 Then CellCount = CellCount + 1 End If Next c ForceDataEntry = False If CellCount <> rngCount Then ForceDataEntry = True End Function
You should note that any implementation that requires macros (like this one does) suffers from one potential problem—users can decide to not enable macros when the workbook is loaded. If they run the workbook with the macros disabled, then they will still be able to save the workbook without all the mandatory cells containing values.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9574) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Requiring Input.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
One way to make your worksheets less complex is to get rid of detail and keep only the summary of that detail. Here's how ...
Discover MoreDeleting rows or columns is easy when you use the shortcut described in this tip. Just select the rows or columns and ...
Discover MoreHave a worksheet in which there may be entire columns that are duplicates of each other? If you want to delete those ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments