How to count words on excel

There is one option that I always wish Excel should have, and that is counting the number of words from a cell. If you work in MS Word there is an inbuilt option on the status bar which shows you how many words are there in the sheet.

there is an option in word to count words but not in excel

But when it comes to Excel there is no such option to count words. You can count the number of cells which have text but not actual words in them. As you know, in Excel, we have functions and you can use them to calculate almost everything. You can create a formula that can count words from a cell.

Today in this post, you will learn how to count words in Excel from a cell, a range of cells, or even the entire worksheet. And I’ll also show you how to count a specific word from a range of cells.

1. Count Words from a Single Cell

To count words from a cell you need to combine the LEN function with the SUBSTITUTE function. And the formula will be (Text is in cell A1):

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1

When you refer to a cell using this formula, it will return 7 in the result. And yes, you have a total of 7 words in the cell.

How it Works

Before getting into this formula just think this way. In a normal sentence if you have eight words then you will definitely have 7 spaces in those words. Right?

That means you will always have one word more than the spaces. The idea is simple: If you want to count the words, count the spaces and add one to them. To understand this formula you need to split it into three parts.

In the first part, you used the LEN function to count the number of characters from cell A1. And in the second and third parts, you have combined SUBSTITUTE with LEN to remove spaces from the cell and then count the characters. At this point, you have an equation like this.

The total number of characters with spaces and the total number of characters without spaces. And when you subtract both numbers get the number of spaces and in the end, you must add one to it. It returns 7 in the result which is the total number of words in the cell.

Important: When you use the above formula it will return 1 even if the cell is blank so it’s better to wrap it with the IF function to avoid this problem.

=IF(ISBLANK(A2),0,LEN(A2)-LEN(SUBSTITUTE(A2," ",""))+1)

This formula will first check the cell and only return the word count if there is a value in the cell.

2. Using a UDF

Apart from the above formulas, I have written a small code to create a UDF for this. This code will help you to create a custom function that will simply return the word count. In short, you don’t need to combine any functions.

Function MyWordCount(rng As Range) As Integer

MyWordCount = UBound(Split(rng.Value, " "), 1) + 1

End Function

Let me tell you how to use it.

  1. First of all, enter this code in VBA editor.
    add-udf-code-to-vbe-to-count-words-in-excel-cell
  2. And then come back to your worksheet, and enter “=MyWordCount(” and refer to the cell in which you have value.

And it will return the word count.

Related: Formula Bar in Excel

3. Count Words from a Range of Cells

Now let’s come to the next level. And here you need to count words from a range of cells instead of a single cell. The good news is you just need to use the same formula (just a simple change) which you have used above. And the formula will be:

=SUMPRODUCT(LEN(A1:A11)-LEN(SUBSTITUTE(A1:A11," ",""))+1)

In the above formula, A1:A11 is the range of cells and when you enter the formula it returns 77 in the result.

How it Works

This formula works in the same way as the first method works but is just a small advance. The only difference is you have wrapped it in SUMPRODUCT and referred to the entire range instead of a single cell.

Do you know that SUMPRODUCT can take arrays? So, when you use it, it returns an array where you have a count of words for each cell. And in the end, it sums those counts and tells you the count of words in the column.

4. Word Count from the Entire Worksheet

This code is one of the useful macro codes which I use in my work, and it can help you to count all the words from a worksheet.

Sub Word_Count_Worksheet()

Dim WordCnt As Long
Dim rng As Range
Dim S As String
Dim N As Long

For Each rng In ActiveSheet.UsedRange.Cells
S = Application.WorksheetFunction.Trim(rng.Text)
N = 0

If S <> vbNullString Then
N = Len(S) – Len(Replace(S, " ", "")) + 1
End If

WordCnt = WordCnt + N

Next rng

MsgBox "There are total " & Format(WordCnt, "#,##0") & " words in the active worksheet"

End Sub

When you run it, it will show a message box with the number of words you have in the active worksheet.

Related: What is VBA in Excel

5. Count a Specific Word/Text String from a Range

Here you have a different situation. Let’s say you need to count a specific word from a range of cells or check the number of times a value appears in a column.

Take this example: Below you have a range of four cells and from this range, you need to count the count of occurrences of the word “Monday”.

For this, the formula is:

=SUMPRODUCT((LEN(D6:D9)-LEN(SUBSTITUTE(D6:D9,"Monday","")))/LEN("Monday"))

And when you enter it, it returns to the count of the word “Monday”. That’s 4.

Important: It returns the count of the word (word’s frequency) from the range not the count of the cells which have that word. Monday is there four times in three cells.

How it Works

To understand this function, again you need to split it into four parts. In the first part, the LEN function returns an array of the count of characters from the cells.

The second part returns an array of the count of characters from the cells by removing the word “Monday”.

In the third part, the LEN function returns the length of characters of wor word “Monday”.

After that, subtracts part one from part two and then divides it with part three it returns an array with the count of the word “Monday” from each cell.

In the fourth part, SUMPRODUCT returns the sum of this array and gives the count of “Monday” from the range.

Download Sample File

  • Ready

Conclusion

Whenever you are typing some text in a cell or a range of cells you can these methods to keep a check on the word count. I wish someday in the future Excel will get this option to count words. But, for the time being, you have all these awesome methods.

Which method do you like the most? Make sure to share your views with me in the comment section, I’d love to hear from you. And please, don’t forget to share this post with your friends, I am sure they will appreciate it.

Want to get the word count in Excel? Believe it or not, Excel does not have an inbuilt word counter.

But don’t worry.

A cool bunch of excel functions (or a little bit of VBA if you’re feeling fancy) can easily do this for you.

In this tutorial, I will show a couple of ways to count words in Excel using simple formulas. And at the end, will also cover a technique to create a custom formula using VBA that will quickly give you the word count of any text in any cell.

Formula to Get Word Count in Excel

Before I give you the exact formula, let’s quickly cover the logic to get the word count.

Suppose I have a sentence as shown below for which I want to get the word count.

Word Count in Excel - formula

While Excel cannot count the number of words, it can count the number of spaces in a sentence.

So to get the word count, we can count these spaces instead of words and add 1 to the total (as the number of space would be one less the number of words).

Now there can be two possibilities:

  1. There is a single space between each word
  2. There are multiple spaces between words.

So let’s see how to count the total number of words in each case.

Example 1 – When there is a single space between words

Let’s say I have the following text in cell A1: Let the cat out of the bag

Word Count in Excel - formula

To count the number of words, here is the formula I would use:

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1

This would return ‘7’ as a result.

Here is how this formula works:

  • LEN(A1) – This part of the formula returns 26, which is the total number of characters in the text in cell A1. It includes the text characters as well as the space characters.
  • SUBSTITUTE(A1,” “,””) – This part of the formula removes all the spaces from the text. So the result, in this case, would be Letthecatoutofthebag.
  • LEN(SUBSTITUTE(A1,” “,“”) – This part of the formula counts the total number of characters in the text that has no spaces. So the result of this would be 20.
  • LEN(A1)-LEN(SUBSTITUTE(A1,” “,“”)) – This would subtract the text length without spaces from the text length with spaces. In the above example, it would be 26-20 which is 6.
  • =LEN(A1)-LEN(SUBSTITUTE(A1,” “,“”))+1 – We add 1 to the overall result as the total number of spaces is one less than the total number of words. For example, there is one space in two words and two spaces in three words.

Get Word Count in Excel - Example 1

Now, this works well if you have only one space character between words. But it wouldn’t work if you have more than one space in between words.

In that case, use the formula in the next example.

Example 2: When there are multiple spaces between words

Let’s say you have the following text: Let the cat   out of    the bag

Word Count in Excel - multiple spaces

In this case, there are multiple space characters between words.

To get the word count, we first need to remove all the extra spaces (such that there is only one space character between two words) and then count the total number of spaces.

Here is the formula that will give us the right number of words:

=LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1

This is a similar formula used in the above example, with a slight change – we have also used the TRIM function here.

Excel TRIM function removes any leading, trailing, and extra spaces (except single spaces between words).

The rest of the formula works the same (as explained in Example 1).

Get Word Count in Excel - Example 2

Note: If there are no spaces between words, it is considered as one word.

Using VBA Custom Function to Count Words in Excel

While the above formulas work great, if you have a need to calculate the word count often, you can use VBA to create a custom function (also called a User Defined Function).

The benefit of using a custom function is that you can create it once and then use it like any other regular Excel function. So instead of creating a long complex formula as we did in the two examples above, you have a simple formula that takes the cell reference and instantly gives you the word count.

Here is the code that will create this custom function to get the word count in Excel.

Function WordCount(CellRef As Range)
Dim TextStrng As String
Dim Result() As String
Result = Split(WorksheetFunction.Trim(CellRef.Text), " ")
WordCount = UBound(Result()) + 1
End Function

Once created, you can use the WordCount function just like any other regular Excel function.

Custom Function Created with VBA to get the word count in Excel

In the above code for the custom function, I have used the worksheet TRIM function to remove any leading, trailing, and double spaces in between words. This ensures that all the three cells give the same result, as only the words are counted and not the double spaces.

How this formula works:

The above VBA code first uses the TRIM function to remove all the leading, trailing and double spaces from the text string in the referenced cell.

Once it has the cleaned string, it uses the SPLIT function in VBA to split the text string based on the delimiter, which we have specified to be the space character. So each word is separated as stored as a separate item in the Result variable.

We then use the UBOUND function to count the total number of items that got stored in the Result variables. Since VBA has a base of 0, we need to add 1 to get the total number of words.

This means that Result(0) stores the first word, Result(1) stores the second word, and so on. Since this counting starts from 0, we need to add 1 to get the real word count.

Where to Put this Code?

When creating a custom function, you need to put the code in the VB Editor of the workbook (which is the back end of the workbook where you can write code to automate tasks and create custom functions).

Below are the steps to put the code for the ‘GetNumeric’ function in the workbook.

  1. Go to the Developer tab.Word Count in Excel - Ribbon for Developer Tab
  2. Click on Visual Basic option. This will open the VB editor in the backend.Click on Visual Basic button in the Excel ribbon
  3. In the Project Explorer pane in the VB Editor, right-click on any object for the workbook in which you want to insert the code. If you don’t see the Project Explorer go to the View tab and click on Project Explorer.
  4. Go to Insert and click on Module. This will insert a module object for your workbook.Saving a Custom Function code in the module
  5. Copy and paste the code in the module window.

Once you have copied the code in the code window, you can go back to your worksheet and use this function just like any other regular Excel function.

Just type =Word and it will show you the formula in the list.

VBA Custom Formula shows up in Worksheet

It takes one argument, which is the cell reference and instantly gives you the word count in it.

You May Also Like the Following Excel Tutorials:

  • How to Count Cells that Contain Text Strings.
  • Using Multiple Criteria in Excel COUNTIF and COUNTIFS Function.
  • How to Add Leading Zeroes in Excel.
  • Remove Spaces in Excel – Leading, Trailing, and Double.
  • How to Extract the First Word from a Text String in Excel

Count Words in a Cell of Excel

There is no in-built excel formula to find the Word Count and therefore, it needs to be entered manually. You can make use of the formula below to calculate wordcount in excel –

=LEN(TRIM(cell))-LEN(SUBSTITUTE(cell,” “,””))+1

Let us understand the working of this formula.

To begin with, we utilize the SUBSTITUTE functionSubstitute function in excel is a very useful function which is used to replace or substitute a given text with another text in a given cell, this function is widely used when we send massive emails or messages in a bulk, instead of creating separate text for every user we use substitute function to replace the information.read more to evacuate and displace all spaces in the cell with a vacant content string (“). The LEN functionThe Len function returns the length of a given string. It calculates the number of characters in a given string as input. It is a text function in Excel as well as an inbuilt function that can be accessed by typing =LEN( and entering a string as input.read more restores the length of the string without spaces.

Next, we subtract the string length without spaces from the absolute length of the string. The number of words in a cell is equivalent to the number of spaces plus 1. So, we add 1 to the word count.

Further, we utilize the TRIM functionThe Trim function in Excel does exactly what its name implies: it trims some part of any string. The function of this formula is to remove any space in a given string. It does not remove a single space between two words, but it does remove any other unwanted spaces.read more to remove extra spaces in a cell. A worksheet may contain a lot of imperceptible spaces. Such coincidental occurrence might be towards the start or end of the text (leading and trailing spaces). Since extra spaces return an incorrect word count, the TRIM function is used before computing the length of the string.

The steps to count the total number of words in a cell of Excel are listed as follows:

  1. Select the cell in the Excel sheet where you want the result to appear.

    Word-Count-Example-1

  2. For counting the number of words in cell A1, enter the formula shown in the following image.

    Word-Count-Example-1-1-1

  3. Click “Enter” and the exact number of words appear in cell B1.

    Word-Count-Example-1-2

Table of contents
  • Count Words in a Cell of Excel
    • How to Count the Total Number of Words in a Range of Cells?
    • How to Count Specific Words in a Range?
    • Frequently Asked Questions
    • Recommended Articles

How to Count the Total Number of Words in a Range of Cells?

To count the number of words in a range of cells, apply the equation that counts the words in a cell and implant it either inside the SUM or the SUMPRODUCT function.

The formula to count words of a particular range is “=LEN(TRIM(cell))-LEN(SUBSTITUTE(cell,” “,””))+1.”

  • Step 1: Select the range of data whose words you wish to count.

Word Count Example 2

  • Step 2: Enter the formula in the cell where you want the result to display as shown in the succeeding image.

Word Count Example 2-1

  • Step 3: Click “Enter” and the result appears in cell B1.

Example 2-2

  • Step 4: Drag the fill handle to all cells to get the word count of each cell in Excel.

Example 2-3

How to Count Specific Words in a Range?

To count the number of times a specific word appears in a range of cells, we utilize a comparative methodology. We count the explicit words in a cell and consolidate it with the SUM or SUMPRODUCT functionThe SUMPRODUCT excel function multiplies the numbers of two or more arrays and sums up the resulting products.read more.

  • Step 1: Select the cell and enter the formula “=(LEN(cell)-LEN(SUBSTITUTE(cell,word,””)))/LEN(word)” as shown in the following image.

Example 4

  • Step 2: Click “Enter” to see the word count in cell A14. The formula we used above helps us know the number of times the word “February” is present in cell A2.

The result in cell A14 is 4.

Example 4-1

Frequently Asked Questions

#1 – How to count the number of times a single character appears in a cell?

The formula to count the occurrence of a single character in a cell is stated as follows:
=LEN(cell_ref)-LEN(SUBSTITUTE(cell_ref,”a”,””))

The “cell_ref” stands for cell reference. The letter “a” stands for the character that the user wants to count.

#2 – How to count the number of times a single character appears in a range of cells?

The formula to count the occurrence of a single character in a range of cells is stated as follows:

=SUM(LEN(range)-LEN(SUBSTITUTE(range,”a”2,””)))

The “range” stands for the range of cells to which the formula is applied. The letter “a” stands for the character that the user wants to count.

#3 – How to count the number of times a specific word appears in a row or a column?

The steps to count the number of times a particular word appears in a row or a column are listed as follows:

1- Select the row or the column in which the word is to be counted. The row is selected by clicking the number on the left-hand side. The column is selected by clicking the letter that appears on top of the column.
2 – In the formula tab, click “Define Name” and enter a name for the row or the column in the “New Name” box.
3 – If the column is named “NamesColumn,” the cells in this column will use “NamesColumn” for reference.
4 – Apply the formula “=COUNTIF(NamesColumn,”Jack”)” to count the number of times “Jack” appears in the “NamesColumn.”

Note: Every time a new name is added to a cell of “NamesColumn,” the result of the formula will automatically update.

  • The formula to count words of a particular range is “=LEN(TRIM(cell))-LEN(SUBSTITUTE(cell,” “,””))+1.”
  • The word count formula is combined with the SUM or SUMPRODUCT function to handle arrays.
  • The SUBSTITUTE function replaces all the spaces of the cell with a vacant content string (“).
  • The LEN function restores the length of the string without spaces.
  • The TRIM function removes the leading and trailing spaces found at the beginning or at the end of the text.
  • The number of words in a cell is equivalent to the number of spaces plus 1.

Recommended Articles

This has been a guide to Word Count in Excel. Here we discuss how to count the total number of words in a cell and an excel range along with practical examples and a downloadable template. You may learn more about Excel from the following articles –

  • Count Rows in Excel | ExamplesThere are numerous ways to count rows in Excel using the appropriate formula, whether they are data rows, empty rows, or rows containing numerical/text values. Depending on the circumstance, you can use the COUNTA, COUNT, COUNTBLANK, or COUNTIF functions.read more
  • Count Colored Cells In ExcelTo count coloured cells in excel, there is no inbuilt function in excel, but there are three different methods to do this task: By using Auto Filter Option, By using VBA Code, By using FIND Method.
    read more
  • Excel Count FormulaThe COUNT function in excel counts the number of cells containing numerical values within the given range. It is a statistical function and returns an integer value. The syntax of the COUNT formula is “=COUNT(value 1, [value 2],…)”
    read more
  • How to Count Unique Values in Excel?In Excel, there are two ways to count values: 1) using the Sum and Countif function, and 2) using the SUMPRODUCT and Countif function.read more
  • Compare Two Lists in ExcelThe five different methods used to compare two lists of a column in excel are — Compare Two Lists Using Equal Sign Operator, Match Data by Using Row Difference Technique, Match Row Difference by Using IF Condition, Match Data Even If There is a Row Difference, Partial Matching Technique.read more

no-override

Reader Interactions

Word Count in Excel – How to Check the Number of Words

Excel does not have its own tool that lets you simply look at the number of words in a document (like Word does). But is it possible to find out anyway?

Yes it is – but it’s a bit convoluted, and it also only works on one cell at a time. Don’t worry, though – we will see at the end how to make it work on a group of cells. Let’s explore how to count words in Excel.

The Excel Functions We’ll Use to Count Characters

We need to learn about three Excel functions, LEN(), TRIM() and SUBSTITUTE(), before we can use them in the formula.

How to Use the LEN() Function in Excel

The LEN() function takes a cell with text content and gives back the number of characters in that cell.

For example, if we write The horse under the three in a cell, and we use the LEN() function to calculate the number of characters in that sentence in another cell, we’ll get 25. You can see how it works here:

len

By specifying that we want the LEN() of cell B1 (LEN(B1), in cell B2 above), Excel does this calculation for us.

Note: I’ll explain why I included the spelling error («three» instead of «tree») below.

How to Use TRIM() Function in Excel

The TRIM() function takes a cell with text content and gives back the same text without any white space at the beginning or end.

For example, say we have a cell that looks like this:       The horse under the three   (with 7 spaces before the text and 2 at the end. It has a total length of 34 characters, including the spaces.

The TRIM() function will give us back The horse under the three (with just the original 25 characters) without the spaces at the beginning or the end. Here’s what it looks like:

trim

You can see that, similar to the example above with LEN(), when we put the TRIM() instructions in cell B4, Excel calculates the correct value in cell B5.

How to Use the SUBSTITUTE() Function in Excel

The SUBSTITUTE() function will replace a piece of the text with another bit of text. For example, in the text we have been using there is a spelling error (instead of tree we have three). We can fix it using the SUBSTITUTE() function.

The syntax is SUBSTITUTE(text, old_text, new_text, [instance_num]), with text being the text we are going to change. In this case, we’ll have the text we want to change, and the old_text and the part we want to change (three) which will be replaced with the new_text (tree).

The complete formula is SUBSTITUTE(B4, "three", "tree"). Note that text in a formula always needs to be put in quotes. Here’s how it works:

substitute

Just in case you need to know, instance_num is an optional parameter that you use in case there are multiple instances of old_text in the text and you want to change only one of them. But we aren’t using it here.

We have learned how to use the above single functions, and now we need to use them together in a somewhat convoluted way.

Before putting them together, let’s try to understand how we are using them, and then we will build together the complete formula.

How Word Count in Excel Works

Excel does not have a proper word count tool or formula, but there is one thing we can count, and that is characters, as we’ve learned above. Specifically, we are going to count the number of spaces inside the string. And from that, we are going to derive the number of words just adding 1 to the number of spaces.

When we look at our example, we can see that the string The horse under the tree has four spaces. If we add one, we get five, the total number of words in the sentence.

Counting spaces is also not a trivial task. Since there is not a specific tool or formula that can count just spaces, we need to be a bit creative.

What we are going to do is count the number of characters in the string, and then count the number of characters in the string when the spaces have been removed (we can use SUBSTITUTE(text, " ", "") for this). Then we’ll take the difference between the two.

The horse under the tree has 24 characters, while Thehorseunderthetree has 20 characters. The difference is 4, which is the number of spaces in the original string. If we add 1 we get 5, the number of words.

count-words

Putting it into Practice

Now we need to put in a single formula that we have seen in the last paragraph. This formula has three components:

  • the length of the sentence with spaces at the beginning or at the end of the sentence removed (we want to count only spaces between the words), so we’ll use LEN(TRIM(text))
  • the length of the string without spaces, in this case, we don’t need to use TRIM() as we are removing all spaces, so LEN(SUBSTITUTE(text, " ", ""))
  • Then we just add 1.

The complete formula is: LEN(TRIM(text)) - LEN(SUBSTITUTE(text, " ", "")) + 1.

single-formula

How to Create a Custom Function to Count Words in Excel

We have learned how to count words in a cell, but maybe we don’t want to type all that every time we need to count the number of words.

Fortunately, we can solve this by creating a custom function to count words. We can also have a custom function to count the total number of words in multiple cells.

How to Create a Custom Function with Visual Basic for Applications

We can open the VBA editor with Alt + F11 (FN + Alt + F11 for Mac). Then we can go to Insert > Module, and we are ready to write our function.

We can use what we have already written as a starting point, but we need to replace SUBSTITUTE, as that doesn’t exist in Visual Basic, with the REPLACE function. So now we’ll have LEN(TRIM(text)) - LEN(REPLACE(text, " ", "")) + 1.

Let’s name the new function we want to create. I have chosen the name WORDCOUNT, but you can use any name you want. Just replace it in the two places it’s written with the name of your choice.

Function WORDCOUNT(text)
   WORDCOUNT = LEN(TRIM(text)) - LEN(REPLACE(text, " ", "")) + 1
End Function

Once you add this code in the editor, you have created the function. Now you can close the editor and enjoy your new function! But keep in mind that it only works for this workbook.

So now let’s make the function work with more than one cell, and then we can also add it permanently to Excel.

How to Build a Custom Excel Function to Count the Total Number of Words in a Group of Cells

We will now update the function to work with a range of cells to make it a bit more useful. We take the same code as above, and apply it to every cell within a range, summing together the number of words in each cell.

Replace the code we wrote before with the following:

Function WORDCOUNT(rng As Range)
    Count = 0
    For Each cl In rng
        thisCount = LEN(TRIM(cl.Value)) - LEN(REPLACE(cl.Value, " ", "")) + 1
        Count = Count + thisCount
    Next
    WORDCOUNT = Count
End Function

Note: this version works with a single range of cells, and all cells selected must contain text. You could try to make your own more versatile version if you want to, just explore VBA on your own!

Lastly, we want to make sure that our function is available in every Excel Work Book. To do that we have to close the VBA editor and save the Book we are working in as *.xlam, the Excel Add-In file type.

To do that we can go to File > Save As, give the file a name that will let us recognize it, like «WordCount», chose the format «Excel Add-In (*.xlam)» from the drop-down menu. Don’t change the folder in which you save the file, as it is set automatically to an AddIns folder.

Now that we have created the file, we need to import it into Excel. To do that we go to File > Options > Add-ins. At the bottom of the window select «Excel Add-ins» from the dropdown menu, and click Go…. In the new window use the Browse… button, and there it should open the AddIns folder we have saved the file in. Select it and press Ok, then Ok again. Now the WORDCOUNT() function will be available every time you use Excel.

Conclusion

In this article, we have learned how to count words in a string in Excel.

And even though Excel does not have a readymade tool for counting words, we learned how to create our own custom function to avoid having to write each formula every time we want to get the number of words in a string.

Finally, we also learned how to expand our function so that it works with any number of cells.



Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Excel doesn’t have a dedicated function for counting words in a cell. However, with a little ingenuity, you can create such a formula using the SUBSTITUTE and LEN functions, with help from TRIM, as shown in the example. At a high level, this formula uses the LEN function to count the number of characters in the cell, with and without spaces, then uses the difference to figure out the word count. This works, because word count is equal to the number of spaces + 1, so long as there is one space between each word.

The first part of the formula counts the characters in cell B5, after removing extra space:

=LEN(TRIM(B5)) // normalize space, count characters

Inside LEN, the TRIM function first removes any extra spaces between words, or at the beginning or end of the text. This is important, since any extra spaces will throw off the word count. In this case, there are no extra space characters, so TRIM returns the original text directly to the LEN function, which returns 30:

LEN("All Quiet on the Western Front") // returns 30

At this point, we have:

=30-LEN(SUBSTITUTE(B5," ",""))+1

Next, we use the SUBSTITUTE function to remove all space characters from the text:

SUBSTITUTE(B5," ","") // strip all space

Notice SUBSTITUTE is configured to look for a space character (» «), and replace with an empty string («»). By default, SUBSTITUTE will replace all spaces. The result is delivered directly to the LEN function, which returns the count:

LEN("AllQuietontheWesternFront") // returns 25

LEN returns 25, the number of characters remaining after all space has been removed. We can now simplify the formula to:

=30-25+1 // returns 6

which returns 6 as a final result, the number of words in cell B5.

Dealing with blank cells

The formula in the example will return 1 even if a cell is empty, or contains only space. This happens because we are adding 1 unconditionally, after counting space characters between words. To guard against this problem, you can adapt the formula as shown below:

=LEN(TRIM(B5))-LEN(SUBSTITUTE(B5," ",""))+(LEN(TRIM(B5))>0)

Notice we’ve replaced 1 with this expression:

LEN(TRIM(B5))>0

This code first trims B5, then checks the length. If B5 contains text, LEN returns a positive number, and the expression returns TRUE. If B5 is empty, or contains only space, TRIM returns an empty string («») to LEN. In that case, LEN returns zero (0) and the expression returns FALSE. The trick is that TRUE and FALSE evaluate to 1 and zero, respectively, when involved in any math operation. As a result, the expression only adds 1 when there is text in B5. Otherwise, it adds zero (0). This logic could also be written with the IF function statement like this:

IF(LEN(TRIM(B5))>0,1,0)

and the result would be the same. The expression above is simply more compact.

Like this post? Please share to your friends:
  • How to count word in page
  • How to count on a formula in excel
  • How to count numbers on excel
  • How to count number of words in word
  • How to count letters in word