Sub topic 16
Limiting Entries with Data Validation
This chapter assumes you can enter and commit data in cells, know how Excel interprets text, numbers, and dates, and understand the difference between a stored value and a displayed value. Data validation builds on those ideas. It checks what you type at the moment you commit it, and it can reject entries that do not meet a rule.
What data validation does
Data validation is a rule attached to one or more cells. When someone types a value and commits it with Enter or Tab, Excel tests the value against the rule. If the value fails, Excel can stop the entry, warn about it, or let it through with a notice.
You find it on the Data tab, in the Data Tools group, under Data Validation. The dialog has three tabs:
- Settings: the rule itself.
- Input Message: a hint that appears when the cell is selected.
- Error Alert: the message and behavior when an entry fails.
Select the target cells first. The rule applies to the selection, not to the whole sheet.
The Settings tab: choosing what is allowed
The Allow list sets the kind of rule. The common choices are below.
| Allow | What it accepts | Typical use |
|---|---|---|
| Whole number | Integers within a condition | Quantity, age |
| Decimal | Any number within a condition | Price, percentage |
| List | One item from a set of choices | Status, department |
| Date | Dates within a condition | Order date, deadline |
| Time | Times within a condition | Shift start |
| Text length | A number of characters | Codes, postal codes |
| Custom | A formula that returns TRUE or FALSE | Anything else |
Whole number, Decimal, Date, Time, and Text length all use a Data condition: between, not between, equal to, not equal to, greater than, less than, and the "or equal to" forms. Between needs a minimum and a maximum. The others need one value.
The values in a rule can be typed constants or cell references. A reference is often better, because you can change the limit in one place. For example, a deadline cell can hold the maximum date, and the rule points to it.
Example: a whole-number quantity
- Select B2:B50.
- Open Data Validation.
- Set Allow to Whole number, Data to between, Minimum to 1, Maximum to 100.
- Click OK.
Typing 250 or 3.5 in any of those cells now fails. Typing 40 passes.
Ignore blank
The Ignore blank box is on by default. When the rule uses cell references, a blank referenced cell then allows any value. If a limit cell is accidentally empty, the rule may stop protecting anything. Clear this box when you want blank limits treated strictly.
Drop-down lists
A list rule gives the user a drop-down arrow in the cell. There are two ways to supply the choices.
Type them in the Source box, separated by commas:
Open,In progress,ClosedOr point to a range of cells, such as =$F$2:$F$4. This is easier to maintain, because you edit the items in the cells and every drop-down updates.
Keep In-cell dropdown checked to show the arrow. Unchecking it hides the arrow but still enforces the list.
A list rule checks typed entries too, not only clicked ones. Typing closed matches Closed when the list is typed directly in the Source box or comes from a range, because the match ignores case in most cases. If the list is typed into the Source box, the match is case-sensitive, so check this in your own version before relying on it.
Lists that grow
A fixed range does not include new items you add below it. Two common fixes:
- Convert the source range to an Excel table and refer to the column through a named range that points to the table column.
- Use a dynamic formula as the source, for example one built with
OFFSETorINDEX.
Either way, test by adding a new item and confirming it appears in the drop-down.
Dependent drop-downs
A second list can depend on the first, such as a city list that changes with the chosen country. The usual method uses INDIRECT with named ranges whose names match the first list's items. It works, but it breaks if the names do not match exactly, so spaces and punctuation in items cause trouble.
Custom rules with formulas
Choose Custom and enter a formula that returns TRUE for valid entries. Write the formula as if it applies to the first cell of your selection, using relative references for the cell itself.
With A2:A100 selected, this rule rejects duplicate entries:
=COUNTIF($A$2:$A$100,A2)=1The range is locked with dollar signs. A2 is relative, so each cell tests its own value.
This rule requires exactly five characters and digits only, which suits codes with leading zeros stored as text:
=AND(LEN(A2)=5,ISNUMBER(--A2))Here --A2 converts the text to a number, and ISNUMBER confirms it is made of digits. Note that a value such as 1E234 also converts, so this rule is not airtight. Combine it with an Allow type of Text length if you need a stricter check.
A common mistake is selecting a range but writing the formula against a different cell. If the active cell is A2 and you write A1, every test is off by one row.
Input messages and error alerts
The Input Message tab shows a small note when the cell is selected. Use it to state the rule in plain words, such as "Enter a whole number from 1 to 100". Keep it short. It disappears when the user moves away.
The Error Alert tab sets the Style and the message.
| Style | Behavior on a failed entry |
|---|---|
| Stop | The entry is refused. The user can retry or cancel. |
| Warning | The user is asked whether to keep the entry anyway. |
| Information | The user is told about the problem and can accept it. |
If you leave Show error alert after invalid data is entered checked and write no message, Excel shows a generic one. A specific message helps more: "Status must be Open, In progress, or Closed."
Use Stop when bad data would break something. Use Warning or Information when the rule is a guideline.
Limits you need to know
Data validation checks typed entries. It does not check everything.
- Pasted data bypasses it. Pasting a value over a validated cell replaces the rule with the source cell's rule, or removes it. Pasting values only keeps the rule but can still insert invalid values in some cases, so do not treat validation as a security feature.
- Fill and drag can overwrite rules. Copying cells with the fill handle copies their validation too.
- Formulas are not checked. A cell whose value comes from a formula is not tested, even if the result breaks the rule.
- Changed limits do not clean old data. If you tighten a rule, existing values stay as they are.
- Rules can be removed. Anyone who can edit the sheet can open the dialog and clear the rule.
To find existing bad entries, use Circle Invalid Data on the Data Validation drop-down. Excel draws red circles around cells that violate their rules. Clear Validation Circles removes them.
To remove a rule, select the cells, open the dialog, and click Clear All. To find which cells have rules, use Home, Find & Select, Data Validation. It selects every validated cell on the sheet. Choose Same in the Go To Special dialog to select only the cells that share the active cell's rule.
Checklist for a good rule
- Pick the narrowest Allow type that fits the data.
- Store limits and list items in cells, not inside the rule.
- Add an input message that states the rule.
- Choose an alert style that matches how strict the rule is.
- Test with a valid value, an invalid value, a blank, and a paste.
Exercise
A column of product codes must be exactly five digits, and leading zeros such as 00123 must be allowed. Codes must not repeat. Describe how you would set this up, including the cell format and the validation rule.
Show answer
Format the column as Text before data entry, or type each code with a leading apostrophe, so leading zeros are kept. Select the data cells, for example A2:A100, and choose Custom in Data Validation with the formula =AND(LEN(A2)=5,ISNUMBER(--A2),COUNTIF($A$2:$A$100,A2)=1). LEN checks the length, ISNUMBER with the double minus checks that the text is numeric, and COUNTIF rejects duplicates. Use the Stop alert style with a message such as "Enter a unique five-digit code."
Exercise
You set a drop-down list on a status column, but a coworker pasted a value that is not on the list and Excel accepted it. Why did this happen, and how can you find such cells?
Show answer
Validation checks only typed entries, and pasting can bypass or replace the rule. To find the invalid cells, open Data Validation on the Data tab and choose Circle Invalid Data. Excel circles every cell whose value breaks its rule.