Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Requiring Input.

Requiring Input

Written by Allen Wyatt (last updated April 1, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


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 even 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.) The two event handlers are put into the code for the workbook and the ForceDataEntry macro is placed in a regular macro module.

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
Sub 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 If
End Sub

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9572) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Requiring Input.

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

Hiding Errors on Printouts

If there are error values in a worksheet, you may not want those error values to appear on a printout. Excel actually ...

Discover More

Defining Protected Sections as AutoText

AutoText is a very useful tool. But what if the text you want to use when creating an AutoText entry is protected and you ...

Discover More

Removing Dashes from ISBN Numbers

ISBN numbers are used to denote a unique identifier for a published book. If you remove the dashes included in an ISBN, ...

Discover More

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!

More ExcelTips (menu)

Entered Values are Divided by 100

Enter a value into a cell and "poof!" it is automatically divided by 100. What's going on, and how can you fix it so that ...

Discover More

Jumping to the Start of the Next Data Entry Row

Want a quick way to jump to the end of your data entry area in a worksheet? The macro in this tip makes quick work of the ...

Discover More

Recording a Data Entry Time

When entering information in a worksheet, it is common to also note a date or time corresponding to the entry. There are ...

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 two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.