Written by Allen Wyatt (last updated October 23, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
Rachel is trying to restrict a cell to only allow entries beginning with at least two digits. Thus, a single-digit entry would not be allowed, but a two-, three-, or four-digit entry would be permissible. Rachel wonders if this is possible, perhaps using Data Validation.
Yes, this can be handled through Data Validation. Assuming that you want to allow only whole numbers to be entered in the cell, you would set it up in this manner:
Figure 1. The Settings tab of the Data Validation dialog box.
If your user is permitted to enter decimal values, then the steps need to be modified slightly:
Either of these approaches will work fine, provided a value such as "03" isn't considered a valid two-digit entry. If this is the case, then you'll need to change entirely the way that the Data Validation is set up:
The drawback to allowing text entry is that, of course, a user could enter a non-sensical value, such as "ab" or "Q7." In addition, since the user is entering a text value (and it is stored in the cell as a text value), you may get funky results if you reference that cell in a formula.
You'll want to carefully consider what you are wanting the user to do when the user enters the data in the cell. You can, if necessary, enter entire formulas in the Data Validation dialog box that evaluate whatever the user enters to make sure it matches your needs. For instance, if you want the user to be able to enter anything provided the first two characters are digits, the following steps will work:
=AND(LEN(A2)>1,LEFT(A2,1)>="0",LEFT(A2,1)<="9", MID(A2,2,1)>="0",MID(A2,2,1)<="9")
Using this scenario, the user could enter "02" or "1234" or even "73abcd." The only requirement is that the first two characters entered would be digits.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6157) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.
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!
When using data validation, you may want to reference a list of validation criteria contained on a different worksheet. ...
Discover MoreData Validation is a great tool in Excel for making sure that whatever is entered in a cell matches your specific ...
Discover MoreData validation is a great tool for limiting what can be input into a cell. Excel allows you to specify what should ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-03-23 08:11:02
zeddy
You could also use a custom function to test a cell's contents.
The custom function would show TRUE if the cell contents matched your rule.
You could then use this cell's result for your Data validation.
For example, this custom function will test whether a cell's content starts with 2 digits:
Function start2digits(zCell)
If zCell Like "##*" Then start2digits = True
End Function
..as you can see, the Excel vba Like function is very versatile, and easily adapted for more intricate cell-content-testing
zeddy
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