Sub topic 9
Write Custom Formula Rules in Google Sheets
Custom formula rules let you define a validation condition with a Google Sheets formula. The formula must return TRUE for an accepted value and FALSE for a rejected value.
This chapter assumes that you can select ranges, enter basic formulas, use relative and absolute references, and refer to another sheet.
How a custom formula is evaluated
Select the cells to validate, open Data > Data validation, and choose Custom formula is. Enter a formula that describes the rule.
When you apply the rule to a range, write the formula as if it applies to the range's top-left cell. Google Sheets adjusts relative references for the other cells.
For example, suppose you apply this rule to A2:A100:
=COUNTIF($A$2:$A$100, A2)=1The range $A$2:$A$100 is absolute, so it stays fixed. The reference A2 is relative, so it becomes A3 for the next row, A4 for the row after that, and so on.
The formula counts how often the current value appears in the complete input range. A value is valid only when it appears exactly once.
Control references carefully
A common error is locking too many references. If you apply a rule to A2:A100, this formula checks only A2 for every row:
=COUNTIF($A$2:$A$100, $A$2)=1Because $A$2 is absolute, it never changes. Use A2 when the formula should refer to the current row.
Use dollar signs for the parts that must remain fixed:
| Reference | Behavior when copied down |
|---|---|
| `A2` | Column and row can change |
| `$A$2` | Neither column nor row changes |
| `$A2` | Column stays fixed; row changes |
| `A$2` | Row stays fixed; column can change |
Validate dates with a formula
Built-in date criteria handle many cases, but a custom formula can combine several conditions. For example, apply this rule to B2:B100 to allow dates from 1 January 2025 through today:
=AND(ISNUMBER(B2), B2>=DATE(2025,1,1), B2<=TODAY())ISNUMBER(B2) confirms that the cell contains a stored date value rather than text that only looks like a date. DATE(2025,1,1) creates the starting date, and TODAY() changes automatically each day.
The AND function returns TRUE only when every condition is true.
Handle blank cells deliberately
Decide whether empty cells should be valid. If a blank is allowed while other values must be unique, use:
=OR(A2="", COUNTIF($A$2:$A$100, A2)=1)If blanks must be rejected, omit the A2="" condition.
Blank handling matters when you apply a rule to an input area before users have filled it. Without an explicit blank condition, a formula may mark empty cells as invalid.
Test and maintain the rule
After saving the rule, test at least one valid and one invalid value. Also test the first, middle, and last rows of the validated range. This exposes incorrect relative references.
Choose Reject input when invalid values must never enter the sheet. Choose Show a warning when users may need to enter unusual values temporarily. Existing invalid data can remain after you create a rule, so validation is not a cleanup tool.
Custom formulas can reference other cells, but they should be easy for the next person to understand. Use a helper column when a long formula becomes difficult to test or explain.
Exercise
You apply a rule to C2:C100. Each cell must contain a number from 1 through 10, or remain blank. What custom formula should you use?
Show answer
Use =OR(C2="", AND(ISNUMBER(C2), C2>=1, C2<=10)). The relative reference C2 changes for each row.