Making Sure Cells are Filled In before Saving

Written by Allen Wyatt (last updated January 21, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Riley has a worksheet that he needs to work with every Friday. This worksheet is based on a template, and there are, at minimum, five cells he needs to fill in, in the range C4:C8. When he starts with the worksheet, these cells are blank. Riley wonders if there is a way to prevent the worksheet from being saved and/or closed until he fills in all five of these cells.

There is a way to do this, but it involves the use of macros. Excel supports the concept of event handlers, which means that you can develop macros that run, automatically, when certain events occur. Two events for which you can create special event handlers are BeforeClose (meaning, before the workbook closes) and BeforeSave (before the workbook is saved).

As an example of how this could work, let's say that the worksheet containing the range to be checked (C4:C8) is named "MyData." You could add this code to the ThisWorkbook module:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim rng As Range
    Dim iCount As Integer
    Dim sTemp As String

    Set rng = Worksheets("MyData").Range("C4:C8")
    iCount = Application.WorksheetFunction.CountBlank(rng)
    If iCount <> 0 Then
        sTemp = rng.Address & " has blank cells. " & vbCrLf
        sTemp = sTemp & "The workbook will not be closed."
        MsgBox sTemp
        Cancel = True
    End If
End Sub

Note that the macro relies on the CountBlank worksheet function to determine if there are any blanks in the range of cells. If it detects any blank cekks (iCount is greater than 0), then the macro displays a message to the user and the Cancel variable is set to True, which actually stops the workbook from closing.

You can use a similar macro for the BeforeSave event, in this manner:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim rng As Range
    Dim iCount As Integer
    Dim sTemp As String

    Set rng = Worksheets("MyData").Range("C4:C8")
    iCount = Application.WorksheetFunction.CountBlank(rng)
    If iCount <> 0 Then
        sTemp = rng.Address & " has blank cells. " & vbCrLf
        sTemp = sTemp & "The workbook will not be saved."
        MsgBox sTemp
        Cancel = True
    End If
End Sub

If you wanted to make sure that the routine selected the cells in which input is needed (as a final step), you could add the following line to both macros, right after the line that sets the Cancel variable to True:

        rng.Select

Remember, as well, that since your workbook is based on a template, it will need to be saved as a macro-enabled template in order to work properly.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (4364) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Stupid Web Toolbar

If the Web toolbar pops up and drives you crazy, this tip can help. Here you learn how to turn off the pop-up nature of ...

Discover More

External Data Validation

When using data validation, you may want to reference a list of validation criteria contained on a different worksheet. ...

Discover More

Removing All Text Boxes In a Document

Text boxes are a common element of many types of documents. At some point you may want to get rid of all the text boxes ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Converting Numbers to Strings

When creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...

Discover More

Clearing the Undo Stack in a Macro

Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...

Discover More

Displaying the First Worksheet in a Macro

When creating macros, you often have to know how to display individual worksheets. VBA provides several ways you can ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2023-01-21 10:57:21

Tomek

To improve on this approach, you can additionally specify data validation for cells in question. That way you can prevent the user from bypassing the requirement to fill the cells by putting just spaces there, as spaces are not counted as blanks. This way you can force the cells to accept only numbers, numbers that are limited in range, dates , times, or only entries that are specified in a list. You can also specify minimum and maximum for text length.
This way you can also limit incorrect information the user can inadvertently put there.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.