Controlling Data Entry in a Cell

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


Murray needs a way to control the entry of data into cell B1. If cell A1 contains the capital letter "A," then the user should be able to enter data into cell B1. If cell A1 contains anything but the capital letter "A," then no data entry should be allowed in cell B1 and cell B1 should show "N/A" (not the error value #N/A, but the letters "N/A").

There are two ways to go about this. One way is to use a macro that checks to see if A1 contains "A" or not. If it does, then the macro keeps whatever is in cell B1, unless B1 had previously been set to "N/A." (If it had, then B1 is cleared.) If A1 does not contain "A," then whatever is in cell B1 is replaced with the characters "N/A."

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim sTemp As String

    If Target.Address(False, False) = "A1" Or _
      Target.Address(False, False_ = "B1" Then

        'Store B1's text in variable
        sTemp = Range("B1").Text

        Application.EnableEvents = False
        If Range("A1").Text = "A" Then
            If sTemp = "N/A" Then Range("B1") = ""
        Else
            Range("B1") = "N/A"
        End If
        Application.EnableEvents = True
    End If
End Sub

Note that this is simply one macro-based approach; there are many other approaches that could be used, depending on what behavior you want to have take place if either cell A1 or B1 are selected. In the case of this macro, it should be saved in the ThisWorkbook module so that it triggers whenever something is changed in the worksheet.

The other approach you can use doesn't involve macros at all. Instead, it relies on conditional formatting. Follow these steps:

  1. Select cell B1.
  2. With the Home tab of the ribbon displayed, click the Conditional Formatting option in the Styles group. Excel displays a palette of options related to conditional formatting.
  3. Click Manage Rules. Excel displays the Conditional Formatting Rules Manager dialog box.
  4. Click New Rule. Excel displays the New Formatting Rule dialog box.
  5. In the Select a Rule Type area at the top of the dialog box, choose Use a Formula to Determine Which Cells to Format. (In Excel 2013 and Excel 2016, click on New Rule and then select Use a Formula to Determine Which Cells to Format.) (See Figure 1.)
  6. Figure 1. The New Formatting Rule dialog box.

  7. In the Format Values Where This Formula Is True box, enter the following: =NOT(EXACT(LEFT(A1,1),"A")). This formula will return True if the cell contains do not contain the letter "A" as the first letter of the cell.
  8. Click Format to display the Format Cells dialog box.
  9. In the Category list, choose Custom.
  10. In the Type box, enter this: "N/A";"N/A";"N/A";"N/A" (make sure you include the quote marks, as shown).
  11. Click OK to dismiss the Format Cells dialog box. The formatting you specified in step 7 should now appear in the preview area for the rule.
  12. Click OK. The New Formatting Rule dialog box disappears and Excel again displays the Conditional Formatting Rules Manager dialog box. The rule you just defined is listed in the dialog box.
  13. Click OK. Excel applies the conditional formatting to cell B1.

The custom format you defined in step 9 causes Excel to display the letters "N/A" when the value is a number (positive, negative, or zero) or text. Since you set all 4 conditions to the same thing, then all of them will display "N/A." This approach changes the display, but it still allows the user to enter a value into cell B1—it just won't display properly unless the first letter in cell A1 is "A."

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13457) 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

Protecting a Single Worksheet

Excel allows you to protect your worksheets easily—and that includes if you need to protect only a single worksheet ...

Discover More

Two Printed Copies to Different Paper Trays

Many modern printers include multiple paper trays that can be used for different types or colors of paper. Word allows ...

Discover More

ExcelTips Ribbon 2022 Archive (Table of Contents)

ExcelTips is a weekly newsletter that provides tips on how to best use Microsoft's spreadsheet program. At the ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Detecting Errors in Conditional Formatting Formulas

If an error exists in a formula tucked inside a conditional format, you may never know it is there. There are ways to ...

Discover More

Noting Inactivity within a Timeframe

There are many times when you are creating a worksheet that you need to analyze dates within that worksheet. Once such ...

Discover More

Working with Multiple Conditions

When you apply conditional formatting, you are not limited to using a single condition. Indeed, you can set up multiple ...

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 4 + 9?

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 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.