Finding Excel Word Count
The summit tip was for a specific product, but opportunities exist where you might want to count words in an Excel cell. I do a similar process when I pull a list of site search queries from my web analytics to see people’s interests. I look at all the cells, but I filter one word entries. The reason is it’s hard for me to guess the searcher’s intent based on one word.
Setting the Stage to Counting Words
The key to counting words in Excel is to correctly identify the spaces between words. You need to remove leading and trailing spaces in the cells or the count will be inflated. There are a couple of ways to do this. A simple way is to use a free Excel add-on I’ve reviewed called ASAP Utilities.
Another way is to use the Excel TRIM function. The trim function removes leading and trailing spaces in a cell. This text function also removes extra spaces between words to just one space. In the table below, you can see that the spaces aren’t always obvious.
Before |
Scenario |
After Trim Function |
Example text |
Leading space |
Example text |
Example text |
Double space |
Example text |
Example text |
Trailing space |
Example text |
In addition to TRIM, I’ll also use Excel’s LEN and SUBSTITUTE functions. These are also considered TEXT functions.
LEN returns the number of characters in a string. In my case, the number will reflect the number for each cell. Since a space is considered a character, it is counted.
SUBSTITUTE is similar to “search and replace” on a cell except we can specify how many times the substitution should occur. For example, you could indicate once, all, or a specific number.
As example, the formula =SUBSTITUTE(A1,"example","sample"), would replace the word “example” with “sample” for cell A1.
For our purposes, we want to substitute a space “ “ with nothing. Effectively, the function removes all spaces so the words run together. “Example text” would change to “Exampletext”.
Understanding the Word Count Formula
One nice feature about Excel is that you can nest formulas that include multiple functions. As example, I’ll use LEN, TRIM and substitute in this formula.
To get the word count in cell A2 in my spreadsheet, I would use this formula in B2,
=IF(LEN(TRIM(A2))=0,0,LEN(TRIM(A2))-LEN(SUBSTITUTE(A2," ",""))+1)
While stringing Excel functions together is efficient, it may make the formula intimidating.
Let me break it down for you.
1. We TRIM any extra spaces in cell A2 and determine if the cell is blank by using =IF(LEN(TRIM(A2))=0,0. If the cell is blank, it assigns the word count as 0.
2. If A2 isn’t blank, we count the characters in the cell using LEN(TRIM(A2)). You might think of this as our starting character count inclusive of spaces. The superfluous spaces have been removed.
3. We use -LEN(SUBSTITUTE(A2)," ","")) to remove the remaining spaces. We then count the characters in this new string.
4. We take the LEN count from Step 2 and subtract the LEN count from Step 3. We then add one to count to adjust for the first word.
If you prefer word problems, think of it this way. If the cell is empty, make the word count = 0. Otherwise, remove the extra spaces and count the characters in the cell. Hold that value as “A”. Now, remove all spaces in that cell and count the characters again. Hold that value as “B”. Your word count is (A-B) + 1.
“Sample example text” = LEN count of 19. This is your “A”.
“Sampleexampletext” = LEN count of 17. This is your “B”.
(19-17)+1 = word count of 3.
After writing this formula, I think I have a new appreciation with the ease at which some programs, like Microsoft Word, can return a word count. If you want to see an example with these formulas, you can download the sample Excel spreadsheet.
Related Excel Tutorials
Last Updated (Friday, 18 June 2010 18:36)