Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Limiting Input to a Format.
by Allen Wyatt
(last updated July 28, 2020)
As you are developing a spreadsheet, you may want to limit what users are able to put into a particular cell. For instance, you might want to make sure that entries in a cell are exactly four characters long, begin with the number 6, are followed by any digit, then by a letter, and then by any digit.
There are three ways to go about such a validation. The first, of course, is to write a macro that will check input and ensure that the entries follow the desired pattern. The way to concoct such a macro has been covered in other issues of ExcelTips.
Another method, without using macros, is to rely upon the data validation feature of Excel. If the number of valid entries is not terribly long, you might try these general steps:
Figure 1. The Data Validation dialog box.
Now, whenever someone tries to enter data that is not included in your list of acceptable values, the validation rules kick into effect and the user is forced to change the entry.
You can also use the data validation feature in a bit of a different way. If you have well-defined rules for your data entry then you can put together a formula that describes a valid entry. The following is one such formula:
=AND((LEN(A1)=4),(LEFT(A1,1)="6"),(ISNUMBER(VALUE( (MID(A1,2,1))))),(CODE(MID(UPPER(A1),3,1))>64),(CODE( MID(UPPER(A1),3,1))<91),(ISNUMBER(VALUE((MID(A1,4,1))))))
Remember that this is a single formula, entered all on one line, using the following general steps:
You may get an error when you click on OK, based on the contents of A1. (Excel may try to tell you that the contents of A1 don't validate according to the rule.) This is OK; the validation rule you are setting up will come into play when someone tries to enter something in the cell.
The formula basically pulls apart the entry being made and determines if each character is within bounds. If not, then the result of the formula is FALSE, and the validation rules come into play.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11212) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Limiting Input to a Format.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Do you need to concatenate the contents of a range of cells in the same column? Here's a formula and a handy macro to ...Discover More
Excel allows you to use multiple colors to format the text in a cell. If you want to later separate that text to ...Discover More
If you hide the first rows of a worksheet, you may have a hard time getting those rows visible again. Here's a simple way ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.