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.

Limiting Input to a Format

Written by Allen Wyatt (last updated August 12, 2024)
This tip applies to Excel 2007 and 2010


2

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:

  1. On a blank worksheet, construct a list of the valid entries.
  2. On the worksheet used for input, highlight the cell into which the entry will be made.
  3. Display the Data tab of the ribbon.
  4. Click the Data Validation tool in the Data Tools group. Excel displays the Data Validation dialog box. (See Figure 1.)
  5. Figure 1. The Data Validation dialog box.

  6. Using the Allow drop-down list, choose List.
  7. With the insertion point in the Source box, choose the list you put together in step 1.
  8. Click on OK.

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:

  1. Highlight the cell into which the entry will be made. (The above formula assumes you are using cell A1. If a different cell is selected, you will need to make the appropriate reference changes to the above formula.)
  2. Display the Data tab of the ribbon.
  3. Click the Data Validation tool in the Data Tools group. Excel displays the Data Validation dialog box.
  4. Using the Allow drop-down list, choose Custom.
  5. Enter the above formula in the Formula box. (All one line, no spaces.)
  6. Click on OK.

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.

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

Getting Rid of Modify Style Message

When you apply styles to a paragraph, you may periodically see a message asking if you want to reapply the style or ...

Discover More

Moving Through a Table in a Macro

Do you need to step through a table, cell by cell, in a macro? It's easy to do using the Move method, as described in ...

Discover More

Returning the Rightmost Value in a Row

Do you need to figure out the rightmost value within a row in which not all cells may contain values? This tip provides a ...

Discover More

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!

More ExcelTips (ribbon)

Dragging to Clear Cells

If you want to get rid of the contents of a range of cells, a quick way to do it is with the Fill handle. Yes, you can ...

Discover More

Entering the Current Time

Need to enter the current time into a cell? It's easy to do using this keyboard shortcut. The shortcut is a handy one to ...

Discover More

Changing References in a Lot of Defined Names

Need to change some cell references in your defined names? Changing one or two is easy; changing dozens is a good deal ...

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 8 + 7?

2016-06-13 08:23:34

Quinn Farley

Can a validation list be any value entered into a multi-column, multi-row array?


2016-06-13 07:59:33

Alex

The List option above can be used for a longer list of valid entries as well.
1). Create the list of valid entries in a separate worksheet.
2). Give the above worksheet a name.
3). With the source field on the Data Validation form selected, navigate to the table created in step 1) above and highlight all entries.
The validation list will then appear as the Validation criteria source, eg =Test!$B$2:$B$5


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.