Count characters and words in Excel with LEN
LEN counts every character in a cell, spaces included. How to count letters, words and one character, and check that codes are the right length.
LEN counts the characters in a cell. =LEN(A3) returns a number,
and every letter, space and dash counts as 1.
Excel has no function that counts words. You count them with LEN too. You take the length of the text, then the length without the spaces, and compare.
Here is a real reason to do it. You sell on an online shop. Before you upload your product list, it must pass 2 rules. Every code has exactly 8 characters. Every title has at most 40. This is the list, in A2 to B7, with a title in A1:
| Row | A | B |
|---|---|---|
| 1 | Upload list | |
| 2 | Code | Title |
| 3 | KT-40125 | Steel kettle 1.7 litre |
| 4 | KT-4013 | Glass teapot with infuser |
| 5 | MG-20931 | Stoneware mug, set of 4 |
| 6 | MG-20932 | |
| 7 | PL-11870 | Bamboo serving tray |
Three things you cannot see from here. A5 has a space at the end. B6 is empty. B7 has 2 spaces at the start and 2 after the word Bamboo.
Count the characters in a cell
In C3, type =LEN(A3). It gives 8. Fill it down to C7. The
results are 8, 7, 9, 8 and 8.
A4 gives 7, so one digit is missing. A5 gives 9 but looks like 8 characters. The ninth one is the space at the end.
That second case is the reason to learn LEN. A space at the end of a cell is invisible. But the
code is now different from the one in your stock system. A lookup on it fails with
#N/A. The
#N/A article lists this as one of the usual
causes.
Check that every code has the right length
=LEN(A3)=8 gives TRUE. On A5 it gives FALSE. For a column that
is easier to read, put the test inside IF:
=IF(LEN(A4)=8,"OK","Check") gives Check. On A3 it gives OK.
To see whether the problem is spaces, compare the length before and after TRIM.
=LEN(A5)-LEN(TRIM(A5)) gives 1. That is 1 extra space. When the
answer is 0, the problem is something else, like a missing digit.
TRIM removes the spaces. The clean-up article shows how to fix a whole column with it.
Codes that are numbers
LEN reads the value in the cell, not what the format shows. Two things follow from that.
- Type 00123 into a normal cell and Excel keeps 123. LEN gives 3, because the zeros are gone. Format the column as Text before you type or paste the codes.
- A price that shows 12.50 holds 12.5. LEN gives 4, not 5. The last zero is only the format.
Check a length limit
=LEN(B4) gives 25. To test the rule of 40, type
=LEN(B4)<=40. It gives TRUE. Fill it down and every FALSE is
a title you must make shorter.
Put the limit in its own cell and point at it, like
=LEN(B4)<=$E$1. If the shop changes the limit to 50, you
change 1 cell.
Count the words in a cell
Count the spaces between the words, then add 1. In D3, type:
=LEN(TRIM(B3))-LEN(SUBSTITUTE(B3," ",""))+1 gives 4.
LEN(TRIM(B3)) is 22, the length with no extra spaces.
SUBSTITUTE(B3," ","") removes every space, and its length is
19. The difference is 3, the number of gaps. 4 words have 3 gaps between them, so you add 1.
The TRIM is there for cells like B7. Without it,
=LEN(B7)-LEN(SUBSTITUTE(B7," ",""))+1 gives 6. The extra spaces
count as gaps. With TRIM, the same cell gives 3, which is right. TRIM removes the spaces at the
start and end. It also turns each group of spaces inside the text into 1 space.
The empty cell that counts as 1 word
Fill the word formula down to B6 and it gives 1. The cell is empty. Both lengths are 0, and the formula still adds 1.
Test for an empty cell first:
=IF(TRIM(B6)="",0,LEN(TRIM(B6))-LEN(SUBSTITUTE(B6," ",""))+1)
It gives 0 for B6. It still gives 4 for B3 and 3 for B7. The TRIM inside the test also catches a cell that holds only spaces. That cell looks empty, but it is not.
Count one character
The same idea works for any character. Remove it, then compare the two lengths.
=LEN(B3)-LEN(SUBSTITUTE(B3,"e","")) gives 5. There are 5 letter
e's in the kettle title.
For codes, count the dashes. =LEN(A3)-LEN(SUBSTITUTE(A3,"-",""))
gives 1. A code with 0 or 2 dashes has the wrong shape.
One trap: SUBSTITUTE sees capital letters as different.
=LEN(B5)-LEN(SUBSTITUTE(B5,"s","")) gives 1. It misses the
capital S in Stoneware. Change the text to small letters first:
=LEN(B5)-LEN(SUBSTITUTE(LOWER(B5),"s","")) gives 2.
What goes wrong in real files
-
Spaces from a web page. Text copied from a website can hold a special space
called a non-breaking space. LEN counts it. TRIM does not remove it, and
SUBSTITUTE(B3," ","")does not find it. Change it to a normal space first withSUBSTITUTE(B3,CHAR(160)," "). - Line breaks. A new line made with Alt+Enter is 1 character. It is not a space, so the word count joins the 2 words on each side of it.
- What a word is. The formula counts gaps. So 1.7 counts as a word, and so does a dash with spaces on both sides.
The LEN, SUBSTITUTE and TRIM lessons below use these same formulas on other data. Each lesson is a real sheet you type into, and each answer is checked when you press Enter.