Write custom validation rules with formulas
Stop bad data at the door8 minPremium
Builds on Allow only sensible entries with validation rules, Count what matches with COUNTIF and COUNTIFS, Combine tests with AND, OR and NOT and Take part of a text with LEFT, RIGHT and MID.
The accounts payable team in Rotterdam logs every supplier invoice in one register. Invoice numbers must start with INV-, and an invoice entered twice gets paid twice.
How it works
Choose Custom under Allow, on the Settings tab of Data Validation, and the rule takes a formula. The formula returns TRUE for an entry that may go in and FALSE for one Excel should refuse. Excel works it out for the cell being typed into, with the new entry already in it.
Write the formula for the active cell, the first one you selected, as if it were going into that cell. Excel moves its relative references to each cell of the range, the way a formula moves when you fill it down, and references with $ signs stay put. So the cell's own entry is a plain reference, and a range every cell must read, such as a column you check for repeats, needs $ signs.
LEFT checks a prefix, COUNTIF counts how often the entry already appears, WEEKDAY gives the day of the week, and a comparison such as > checks the entry against another cell in its row. AND puts two tests into one rule.
Try the test in an empty helper column first and fill it down. Its TRUE and FALSE results show which entries the rule would let in and which it would refuse. Then type it in the Formula box and delete the helper.
Example
Deliveries to a warehouse in Tilburg can only be booked Monday to Friday. With 2 as its second argument, WEEKDAY numbers Monday 1 through Sunday 7, so less than 6 means a weekday:
=WEEKDAY(B2,2)<6Common mistakes
=WEEKDAY(as the rule. Every cell then tests B2's date, so a Saturday typed in B9 goes in whenever B2 is a weekday. Keep the cell's own reference relative.$B$2, 2)< 6 - A range without $ signs in a repeat check. It slides down with each cell, so a lower cell only counts the entries below it.
- A rule written for the wrong row. With
B2:B30selected and B2 active,=WEEKDAY(makes each cell test the date three rows below it.B5, 2)< 6
In short
- Custom takes a formula that's TRUE for an entry that may go in.
- Write it for the active cell: the entry's own cell relative, shared ranges with $ signs.
- Test it in a helper column first, then type it in the Formula box.
Practice
Premium4 exercises: 1 formula, 2 data validation rules and 1 quiz question.
Practice is part of Premium
Every lesson is free to read, and you can practice the free lessons without an account. This lesson's exercises are part of Premium, which you add to a free account.
Have one? Sign in