Cellwise Start free

Excel guide · 6 min read

How to use a custom formula in Excel Data Validation

Quick answerSelect the cells, choose Data > Data Validation, set Allow to Custom and type a formula that returns TRUE for a valid entry, written for the active (first) cell. For example, =COUNTIF($A$2:$A$100,A2)=1 selected on A2:A100 rejects any ID already in the list. Excel accepts the entry when the formula is TRUE and shows an error when it is FALSE.

Sample data

Every result on this page was calculated from this sheet by the same spreadsheet engine the Cellwise app uses.

ABCDEFGH
1Customer IDPhoneOrder dateUnitsOrders fromOct 1, 2026
2CU104822025550143Oct 12, 202624Orders toOct 31, 2026
3CU105173125550198Oct 14, 202636
4CU104826175550122Oct 15, 202612
5cu10533202555014Oct 20, 202630
6CU1059(312) 555-0177Nov 15, 202620
7CU106014155550160Sep 28, 202648
8CU1060A415.555.0160Oct 3, 20266

Download this sheet as an Excel practice file (.xlsx)

Block duplicates with COUNTIF

The sheet is an order log: customer ID, phone, order date and units. The most common custom rule is "no ID twice". COUNTIF counts how many cells in the list match the new entry, and that count includes the entry itself, so a unique ID gives 1 and a repeat gives 2 or more.

Before you add a rule, test it as a normal formula in a spare column next to the data. It returns the same TRUE or FALSE that Data Validation will use. Row 4 repeats CU10482 from row 2, so the test is FALSE. In a helper column row 2 is FALSE as well, because there are two copies by then. With the rule in place, Excel would have accepted row 2 and refused row 4 when someone typed it.

COUNTIF ignores case, so cu10482 counts as a copy of CU10482 (COUNTIF(A2:A8,"cu10482") returns 2). That is usually what you want for IDs. One trap: COUNTIF treats text that looks like a number as a number, and Excel numbers keep only 15 digits, so 16-digit IDs that differ only in the last digit count as duplicates. For long numeric IDs use =SUMPRODUCT(--($A$2:$A$100=A2))=1 instead.

  1. Select A2:A100, starting from A2 so it is the active cell.
  2. Go to Data > Data Validation and open the Settings tab.
  3. Set Allow to Custom and type =COUNTIF($A$2:$A$100,A2)=1 in the Formula box.
  4. On the Error Alert tab, keep Style on Stop and write a message such as "This customer ID is already in the list." Click OK.
=COUNTIF($A$2:$A$100,A4)=1

Result: FALSE

FALSE: CU10482 is already in row 2, so this entry would be rejected. A3 (CU10517) gives TRUE.

Relative and absolute references in the rule

Excel reads the formula as written for the active cell, the white one in the selection, and adjusts it for every other cell just as if you had filled it down. That is why the rule above has $A$2:$A$100 locked (every cell checks the same list) and A2 relative (each cell checks itself). Leave the list unlocked and A50 ends up checking A50:A148, so duplicates higher up slip through.

The active cell is the one you started the selection from. If you select upwards from A100, the active cell is A100 and the formula must say A100, not A2. Typing A2 there makes every cell check the value 98 rows above it. When a rule behaves strangely, open it from a cell in the middle of the range: Excel shows the formula adjusted for that cell, so you can see which cell it really tests.

Lock the column but not the row when a rule looks at another column in the same row. =$A2<>"" applied to D2:D100 means "units can only be entered once the row has a customer ID".

Exact length and digits only

For a 10-digit phone number, LEN checks the length. The usual extra test is ISNUMBER(--B2), where -- turns digit text into a number and anything else into an error. That stops "(312) 555-0177" in row 6 (14 characters) and the 9-digit entry in row 5. But it lets through text that Excel can read as a number: "12345678.9" and "-123456789" both have 10 characters and both pass.

For a strict digits-only rule, test each character. MID(B2,ROW($1:$10),1) splits the cell into its first 10 characters, -- turns each digit into a number, and SUMPRODUCT counts how many worked. Only all 10 digits gives 10; "12345678.9" scores 9. ROW($1:$10) must keep its $ signs so it stays 1 to 10 in every cell.

Phone numbers and IDs that start with 0 need the column formatted as Text before anyone types. Typed into a General cell, 07700900123 becomes the number 7700900123, which is 10 characters, and the zero is gone. The same goes for card and order numbers longer than 15 digits.

=AND(LEN(B5)=10,SUMPRODUCT(--ISNUMBER(--MID(B5,ROW($1:$10),1)))=10)

Result: FALSE

FALSE: B5 has only 9 digits. Rows 2, 3, 4 and 7 return TRUE; rows 6 and 8 fail on length (14 and 12 characters).

Require a code pattern such as CU + 5 digits

Customer IDs here are CU followed by five digits. Check the length, the prefix and that the last five characters are a number. RIGHT(A2,5) on CU1060A gives "1060A", -- turns it into an error, and ISNUMBER returns FALSE, so the rule fails cleanly instead of erroring.

Text comparisons in Excel ignore case: LEFT(A5,2)="CU" is TRUE for cu10533. If the prefix must be capitals, use EXACT, which is case-sensitive, as below. Row 6 (CU1059) fails on length, row 8 (CU1060A) on the digits.

Combine it with the duplicate rule in one formula: =AND(COUNTIF($A$2:$A$100,A2)=1,LEN(A2)=7,EXACT(LEFT(A2,2),"CU"),ISNUMBER(--RIGHT(A2,5))). A cell can only have one validation rule, so several checks have to go into one AND. Put the reason in the error message, since Excel cannot tell the user which part failed.

=AND(LEN(A5)=7,EXACT(LEFT(A5,2),"CU"),ISNUMBER(--RIGHT(A5,5)))

Result: FALSE

FALSE, because cu10533 starts with lowercase letters. With LEFT(A5,2)="CU" instead of EXACT, the same entry passes.

Dates inside a window, weekdays only

The built-in Date option can check "between two dates", but it cannot add other conditions. A custom rule can. Keep the window in input cells (H1 and H2 here) so changing the month means editing two cells, not the rule. Row 6 (15 November) and row 7 (28 September) fall outside October and fail.

Adding WEEKDAY(C2,2)<6 blocks weekend dates. Row 8, Saturday 3 October, is inside the window but fails this rule. The same pattern handles "a whole multiple of 6" for units sold in packs: =MOD(D2,6)=0 rejects the 20 in row 6.

Rules built on TODAY(), such as =C2<=TODAY() for "no future dates", are checked only at the moment of entry. A date that was valid last month stays in the cell; Excel does not go back and complain.

=AND(C8>=$H$1,C8<=$H$2,WEEKDAY(C8,2)<6)

Result: FALSE

FALSE: 3 October 2026 is in the window but falls on a Saturday. Without the WEEKDAY test, the same date passes.

Why bad data still gets in, and how to find it

Data Validation only checks what someone types or picks in a cell. It does not check data that was already there when you added the rule. Pasting gets around it too: a normal paste replaces the cell's rule with whatever the copied cell had, and Paste Special > Values pastes without checking. Error Alert styles Warning and Information also let people click past the message. Only Stop actually refuses the entry.

To find the cells that break their rule, use Data > Data Validation (the arrow on the button) > Circle Invalid Data. Excel draws a red circle around each one; Clear Validation Circles removes them. For a report, an audit formula does the same job and can be re-run after every import.

=FILTER(A2:A8,(COUNTIF(A2:A8,A2:A8)>1)+(LEN(B2:B8)<>10)+(C2:C8<H1)+(C2:C8>H2)+(MOD(D2:D8,6)<>0))

Result: spills 6 rows × 1 column

CU10482
CU10482
cu10533
CU1059
CU10601
CU1060A

CU10482, CU10482, cu10533, CU1059, CU10601 and CU1060A: the six rows that break at least one rule. Only CU10517 is clean.

Common mistakes

Writing the rule for the wrong cell

The formula must refer to the active cell of the selection. Select from the top (A2) and write A2, or match whatever cell is active.

Forgetting $ on the list range

Lock it, as in $A$2:$A$100. Otherwise the range slides down with each cell and duplicates above are missed.

Expecting the rule to catch pasted or existing data

It only checks typed entries. Use Circle Invalid Data or an audit formula after pasting or adding a rule.

Testing a code prefix with = and expecting case to matter

="CU" ignores case. Use EXACT(LEFT(A2,2),"CU") when capitals are required.

FAQ

How do I use a custom formula in Data Validation?

Select the range, go to Data > Data Validation, set Allow to Custom and enter a formula that returns TRUE for valid input, written for the active cell, such as =LEN(A2)=10.

How do I prevent duplicate entries in Excel?

Apply the custom rule =COUNTIF($A$2:$A$100,A2)=1 to A2:A100. Excel refuses an entry that already appears in the range. For IDs longer than 15 digits, use =SUMPRODUCT(--($A$2:$A$100=A2))=1.

Why is my Data Validation not working?

Usually the formula refers to the wrong cell for the active cell, the list range is not locked with $, or the data was pasted rather than typed. Pasting can replace or bypass the rule.

Can a Data Validation rule check more than one condition?

Yes. A cell has one rule, so combine the tests with AND (all must pass) or OR (any may pass), such as =AND(LEN(A2)=7,ISNUMBER(--RIGHT(A2,5))).

How do I find cells that break the validation rule?

Choose Data > Data Validation > Circle Invalid Data. Excel circles every cell whose value fails its rule, including values pasted in or entered before the rule existed.

Functions used

Related guides

Updated