# How to check the length of text with IF in Excel

Compare LEN with the required length: =IF(LEN(A2)=$E$1,"OK","Wrong length").

**Excel:** `=IF(LEN(A2)=$E$1,"OK","Wrong length")` (Every current version)

**Google Sheets:** the same formula works.

## Example

Sample data (row 1 is the header):

| A | B | C | D | E |
| --- | --- | --- | --- | --- |
| Account ID | Check |  | Required length | 8 |
| AC104522 |  |  |  |  |
| AC10452 |  |  |  |  |
| AC1045229 |  |  |  |  |
| AC204118 |  |  |  |  |
| AC 3041 |  |  |  |  |
| AC509931 |  |  |  |  |

Formula in B2, filled to B7: `=IF(LEN(A2)=$E$1,"OK","Wrong length")`

Result: OK; Wrong length; Wrong length; OK; Wrong length; OK

## How it works

LEN counts every character, letters, digits, spaces and punctuation alike. IF compares the count with the required length kept in `E1`. "AC 3041" has 7 characters including the space.

## Other ways

- `=IF(LEN(TRIM(A2))=$E$1,"OK","Wrong length")`: Ignores leading and trailing spaces, which imports often add.
- `=LEN(A2)`: The length itself, to see how far off each ID is. (Shows 8, 7, 9, 8, 7 and 8.)

## Common mistakes

- LEN counts spaces and punctuation, so a stray space makes a valid-looking ID the wrong length.
- Numbers lose leading zeros: an ID typed as 00123 is stored as 123, which has 3 characters. Store IDs as text.

## Questions

**How do I allow a range of lengths?**

`=IF(AND(LEN(A2)>=8,LEN(A2)<=10),"OK","Wrong length")`.

**How do I enforce the length while typing?**

Use Data Validation with Text length, equal to 8.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/check-text-length-with-if
