DATA CLEANUP & ERROR CHECKS

Data Validation with a Custom Formula in Excel (Stop Bad Entries)

A drop-down list stops typos in a category column, but most bad data is something else: an invoice entered twice, a date in the wrong month, a vendor code with a letter where a digit should be, an amount with three decimals. Data Validation with a custom formula blocks all of these at the moment they are typed. This guide builds four practical rules on a supplier invoice sheet, then adds the one check that Data Validation cannot do on its own: catching values that were pasted in.

Excel data validation with a custom formula that refuses a duplicate invoice number, with rules for dates, vendor codes and amounts and 5 rows to fix.
Four rules guard the entry columns.

Quick answer

  1. Select the input range, starting with its first cell (for example B7:B106).
  2. Data > Data Validation > Settings > Allow: Custom.
  3. Type a formula that returns TRUE for a valid entry, written for the first cell: =COUNTIF($B$7:$B$106,B7)=1.
  4. On the Error Alert tab, choose Stop and write a clear title and message.
  5. Use Data > Data Validation > Circle Invalid Data to find entries that were pasted in.
Free download: Get the practice workbook (.xlsx). It has the four rules already set up on Entries!A7:D106, check columns that apply the same rules to every row, and 4 pasted rows that break them. No macros; works in Excel 2010 and later, Microsoft 365, Excel for the web and LibreOffice.

Step 1: Set up the entry sheet

The example is a supplier invoice log for November 2026: date, invoice number, vendor code and amount, one invoice per row. The period start and end sit in B3 and B4 so the date rule can refer to them and you only change two cells next month. Yellow cells are inputs. Leave room below the data (here down to row 106) and apply the rules to the whole input range, not just the filled rows.

Excel supplier invoice entry sheet for November 2026 with 20 rows of dates, invoice numbers, vendor codes and amounts, and the period Nov 1 to Nov 30 in B3 and B4.
Inputs in yellow, period in B3:B4.

Step 2: Rule 1, no duplicate invoice numbers

=COUNTIF($B$7:$B$106,B7)=1

Select B7:B106 with B7 as the active cell, open Data > Data Validation, choose Allow: Custom and type the formula. A custom rule is any formula that returns TRUE (allow) or FALSE (refuse). Write it for the first cell only: the range is absolute ($B$7:$B$106) so it stays fixed, and B7 is relative so Excel checks B8 in row 8, B9 in row 9 and so on. The count includes the value you are typing, so 1 means it is the only one.

Excel Data Validation dialog with Allow set to Custom and the formula =COUNTIF($B$7:$B$106,B7)=1 for the invoice number column.
Allow: Custom, formula for the first cell.
Relative references: If you select the range from the bottom up, the active cell is B106 and the formula must be written for B106. Select from the top so the formula is written for B7, or the rule checks the wrong cells.

Step 3: Write an error alert people understand

On the Error Alert tab, keep Style: Stop and give the alert a title and message that say what is wrong and what to do: Duplicate invoice number, This invoice number is already in the list. Excel's default message only says the value does not match the restrictions. Stop refuses the entry; Warning and Information let the user continue after a prompt, which is useful for soft limits such as an unusually large amount. The Input Message tab adds a tip that appears when the cell is selected.

Excel error alert titled Duplicate invoice number refusing a second INV-24002 typed in cell B27, with Retry, Cancel and Help buttons.
A clear title and message.

Step 4: Rules for dates, codes and amounts

Repeat the same steps for the other columns, each with its own formula:

ColumnCustom formulaAllows
Date (A7:A106)=AND(ISNUMBER(A7),A7>=$B$3,A7<=$B$4)Real dates inside the period in B3:B4
Vendor code (C7:C106)=IFERROR(AND(LEN(C7)=6,EXACT(LEFT(C7,2),"V-"),RIGHT(C7,4)=TEXT(--RIGHT(C7,4),"0000")),FALSE)V- followed by exactly 4 digits
Amount (D7:D106)=AND(ISNUMBER(D7),D7>0,D7=ROUND(D7,2))Positive numbers with at most 2 decimals

The vendor code rule is the interesting one. LEN checks there are 6 characters, EXACT makes the V- prefix case-sensitive (v-1004 is refused), and the last part turns the final 4 characters into a number and back into 4-digit text: only real digits survive the round trip, so V-12A4, V-1E10 and V-12.5 all fail. IFERROR turns the error from non-numeric text into FALSE. ISNUMBER in the date and amount rules refuses text that only looks like a date or number.

Excel Data Validation custom formula for vendor codes that checks the length, the V- prefix and 4 digits, with the date and amount rules listed beside it.
One formula per column.

Step 5: Find entries that were pasted in

Data Validation only checks what is typed. Pasting a value over a cell skips the check, and a normal paste even replaces the cell's validation rule with the source cell's settings. To keep the rules, paste with Paste Special > Values (see Paste Special shortcuts), and to find what got through, choose Data > Data Validation (the arrow) > Circle Invalid Data. In the sample, rows 12, 15, 18 and 21 were pasted in, and Excel circles 5 cells: both copies of INV-24002 (each one now has a duplicate), V-12A4, 1,250.555 and an October date. Clear Validation Circles removes them.

Excel Circle Invalid Data marking 5 cells with red circles: both INV-24002 invoice numbers, the code V-12A4, the amount 1,250.555 and the date Oct 27.
Excel circles every cell that breaks its rule.

Step 6: Keep the rules visible with check columns

Circles vanish when you close the file, so for a log that other people fill in, put the same formulas in check columns. Columns G:J use each rule wrapped in IF(COUNTA($A7:$D7)=0,"", … ) so empty rows stay blank, column K shows OK or Fix, and J4 counts the rows to fix. The sample has 20 rows and 5 to fix, so the status says Check. Conditional formatting colours the FALSE cells red, the same technique as conditional formatting based on another cell.

Excel check columns showing TRUE or FALSE for each rule on every row and OK or Fix per row, with 5 of 20 rows to fix.
5 of 20 rows to fix.
Try it: Change B12 (the second INV-24002) to INV-24006: Rows to fix drops from 5 to 3, because both copies of the duplicate were flagged. Then type V-12A4 in an empty Vendor code cell such as C27: Excel refuses it with the Invalid vendor code alert.

More custom formula examples

GoalCustom formula (first cell A2)
Whole number from 1 to 40 (hours)=AND(ISNUMBER(A2),A2=INT(A2),A2>=1,A2<=40)
Text with no leading or trailing spaces=A2=TRIM(A2)
Only allow an entry when B2 is filled=B2<>""
Weekday dates only=WEEKDAY(A2,2)<6
Column total cannot exceed a budget in F1=SUM($A$2:$A$100)<=$F$1

For lists of allowed values use a drop-down instead: see data validation list from a table. To clean spaces that are already in the data, see remove extra spaces and hidden characters.

Common mistakes

  • Writing the formula for the wrong cell (the active cell must match the relative reference).
  • Forgetting the $ signs on the range in COUNTIF, so the range moves down row by row.
  • Expecting validation to stop pasted values. It does not; use Circle Invalid Data or check columns.
  • Rules that return text or a number instead of TRUE or FALSE.
  • A vague error message that does not say how to fix the entry.

Frequently asked questions

How do I use a custom formula in Data Validation?

Select the range, choose Data > Data Validation > Allow: Custom, and type a formula that returns TRUE for valid entries, written for the active (first) cell of the range.

Can data validation refer to another cell?

Yes. The date rule uses the period cells B3 and B4 with absolute references. A custom formula can refer to any cell, including cells on other sheets in Excel 2010 and later.

How do I prevent duplicates with data validation?

Use =COUNTIF($B$7:$B$106,B7)=1 on the column. For a list that already contains duplicates, find them first with a duplicate check such as the one in the duplicate invoice numbers post.

Why does Excel allow a value that breaks my rule?

It was pasted, filled with Ctrl+D or imported, or the cell was empty and Ignore blank is ticked. Run Circle Invalid Data to find such cells.

Does custom data validation work in Excel for the web?

Yes. Excel for the web applies existing rules and lets you create and edit them in Data > Data Validation.

Related checks: flag duplicate invoice numbers, compare two lists and Excel's error checking rules.

Final checklist

  • Rules applied to the whole input range, written for the first cell.
  • Absolute ranges ($B$7:$B$106) and relative cell references (B7).
  • Stop alerts with a clear title and message.
  • Circle Invalid Data run after pasting or importing.
  • Check columns show 0 rows to fix before the log is used.
Workbook: Download the practice workbook and try to break the rules.

Clean data from the first entry

Find more ready-to-use Excel templates, learn the formulas in the OneXcel Tutorial Hub, or turn clean data into a report with the OneXcel Dashboard Builder.