MENTOR ME CAREERS

LEN Function in Excel: Formula, Examples and Text Checks

Last updated on November 18th, 2026 at 03:23 pm

The LEN function in Excel counts characters in a value, including spaces and punctuation. Enter =LEN(A2) to count the characters in A2. For example, =LEN("AML 2026") returns 8: three letters, one space and four digits. LEN returns a number; it does not remove or change the text.

That small check can explain a failed lookup, catch a short customer reference or flag a label that will not fit a reporting template. The important step is to define what a valid entry looks like. A string with eight characters can still contain the wrong eight characters. This tutorial moves from simple counts to those practical checks, with formulas you can copy into a blank workbook.

LEN function in Excel: counting spaces and reference characters
A trailing space changes an eight-character reference into a nine-character string.

LEN function in Excel: syntax and first examples

The syntax is =LEN(text). Its single required argument can be a cell reference, a number or text enclosed in double quotation marks. Type the formula in a different cell from the value you want to inspect. If your Excel installation uses semicolons between function arguments, adapt the combined formulas below to that regional setting.

Input or formulaResultWhat the result tells you
=LEN("Finance")7Seven letters.
=LEN("AML 2026")8The space counts too.
=LEN("ID-0042")7The hyphen and both zeroes count.
=LEN("")0An empty text string has no characters.
=LEN(" ")1A space is not an empty string.

For a worksheet exercise, put AML 2026 in A2 and =LEN(A2) in B2. Change A2 to AML-2026. The result remains 8 because a hyphen replaced the space. LEN detects length, not whether the separator is correct.

Why LEN counts spaces you cannot easily see

A leading or trailing space can make two references look identical while their underlying text differs. Suppose A2 contains ACME-104 and A3 contains the same reference followed by a normal space. Their lengths are 8 and 9. This is a useful diagnostic when data arrives from a CSV export, a pasted email or another system.

For a space check with the LEN function in Excel, use =LEN(A2)-LEN(TRIM(A2)) to compare the raw length with the length after TRIM removes ordinary leading and trailing spaces and reduces repeated ordinary internal spaces to one. A positive result tells you that TRIM changed the text. It does not tell you that the cleaned value is an authorised customer identifier.

Exclude all ordinary spaces, or only tidy them?

These operations serve different purposes. =LEN(SUBSTITUTE(A2," ","")) counts characters after removing every ordinary space. By contrast, =LEN(TRIM(A2)) counts a tidied phrase that still has single spaces between words. For AML KYC, the results are 6 and 7 respectively. Decide whether spaces are valid data before stripping them.

Copied web text may contain a nonbreaking space, which TRIM alone does not remove. For the common CHAR(160) case, test =LEN(TRIM(SUBSTITUTE(A2,CHAR(160)," "))). This replaces that character with an ordinary space before trimming. Keep the raw data in a separate column so you can audit the change. Other Unicode spacing characters need their own diagnosis.

LEN function in Excel: validate an eight-character reference

Imagine a fictional invoice file where every reference must contain exactly eight characters. A2 contains AB004217. In B2, enter =IF(LEN(A2)=8,"Length OK","Check reference"). Copy it down the file. A missing digit or an extra trailing space should trigger the check.

Reference stored as textLENLength test
AB0042178Length OK
AB004217Check reference
AB004217 followed by a space9Check reference
XXXXXXXX8Length OK, but identity is not verified

The last row is the control that matters. Length alone cannot prove that a reference exists, uses the correct characters or belongs to the right account. Combine this flag with a lookup against the approved reference list and any documented format rule. Never remove leading zeroes from an identifier just because Excel can convert it into a number.

Limit entries to 40 characters

To restrict new entries, select a range with A2 as its active cell. Open Data Validation, choose a Text Length rule and set the maximum to 40. A custom rule can instead use =LEN(A2)<=40. Test a 40-character entry and a 41-character entry. Validation settings and pasted/imported data need separate checks; a worksheet control is not a substitute for validation in the source system.

LEN function in Excel with RIGHT: remove a prefix

To combine the LEN function in Excel with RIGHT, put INV-004217 in A2. Suppose you need the six-digit suffix. There are ten characters in total. =RIGHT(A2,LEN(A2)-4) takes the final six and returns 004217 as text. LEN supplies the length, while RIGHT performs the extraction.

That formula assumes every valid entry has a four-character prefix. Check that assumption before applying it to a mixed file. If you need the text after the first hyphen, =RIGHT(A2,LEN(A2)-FIND("-",A2)) uses the separator position instead. FIND returns an error when the hyphen is missing, which can be a useful exception to investigate rather than silently hiding it.

To answer the common “remove three characters from the left” question, use =RIGHT(A2,MAX(0,LEN(A2)-3)). MAX prevents a negative length when the input is shorter than three characters. That behaviour may be unsuitable for your process: if short inputs are invalid, flag them instead of quietly returning an empty string.

Example 3: count a particular character

The LEN function in Excel can measure what disappears when SUBSTITUTE removes a character. For ANNA in A2, =LEN(A2)-LEN(SUBSTITUTE(A2,"N","")) returns 2. The starting length is four; removing uppercase N leaves two characters. The difference is therefore two occurrences.

This example is case-sensitive because SUBSTITUTE matches the supplied text. To count n and N together in ordinary Latin text, apply UPPER to both versions of the input: =LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"N","")). For a multi-character search term, divide the difference in lengths by the length of the term. This counts non-overlapping matches, not every possible overlapping pattern.

Numbers, dates and emoji: what LEN actually measures

Cell formatting can make the visible value longer than its underlying value. A numeric 1000 displayed as 1,000 still normally gives a LEN result of 4. A numeric value displayed with leading zeroes has not become a text identifier. If the requirement concerns a formatted output, create that output explicitly with TEXT, then measure the resulting text.

Dates create a similar trap because Excel stores them as serial numbers. Do not use LEN to decide whether a date is valid or whether its displayed date format has ten characters. Use a date rule for date validity and TEXT only when you intentionally need to inspect a particular display string.

Microsoft 365 also has workbook compatibility settings that affect some Unicode characters. In Compatibility Version 2, LEN counts a Unicode surrogate pair as one character; older behaviour can count it as two. A combined emoji may still contain several components. Check the workbook setting and the destination system before using an Excel length test for a strict international text limit. Microsoft explains the compatibility-version distinction.

Fixing unexpected LEN results

SymptomLikely issueUseful next check
Count is one too highA trailing space or hidden characterCompare the raw and cleaned lengths; inspect the original export.
Leading zeroes disappearedIdentifier imported as a numberRe-import that column as text; do not guess missing digits.
Formula returns #NAME?Misspelt function or unquoted literal textUse LEN, not LENGTH; put literal strings in double quotes.
An error in A2 also appears in the resultLEN is receiving an error valueTrace the upstream formula before masking the error.
Result changes between workbooksUnicode compatibility or different dataCheck the raw value and workbook compatibility setting.

LEN function in Excel: a practice exercise

Create five rows: BANK-01, BANK 01, BANK-01, BANK-01 and an empty cell. Predict the raw counts before calculating them. The answers are 7, 7, 8, 8 and 0. Then compare the raw count with TRIM and SUBSTITUTE. Explain why the first two raw counts match even though their contents differ.

If you are building spreadsheet controls for reporting, pair this exercise with our Excel MOD function guide and free Excel course. For a wider learning path, review the advanced Excel syllabus. The goal is to make a rule explicit, test its boundary cases and keep an audit trail of changes.

LEN function in Excel: frequently asked questions

What is the use of the LEN function?

Use LEN to count characters, test text-length rules and supply lengths to text-extraction formulas. It counts spaces and punctuation as well as letters and digits. It does not check whether the content is accurate.

Which category does LEN belong to?

LEN is an Excel text function. Its result is numeric, but its purpose is to inspect the length of text. It is not a financial calculation function.

Does LEN count blank cells?

A reference to an empty cell returns zero. A formula returning an empty string also has length zero, while a cell containing a space has length one. Use ISBLANK if you need to distinguish a truly empty cell from a cell containing a formula.

Can LEN remove characters?

No. Combine LEN with RIGHT, LEFT or another text function to construct the desired output. Keep the original value and check the prefix or separator rule before changing a full column.

Where can I check the official syntax?

See Microsoft’s LEN documentation and its character-counting examples. The worked business controls in this guide are fictional teaching examples, not a statement about any employer’s systems.