Explanation
In the generic version of the formula, rng represents the range to check, and txt is the word or substring to count.
In the example shown, B5:B8 is the range to check, and C2 contains the text (word or substring) to count.
For each cell in the range, SUBSTITUTE removes the substring from the original text and LEN calculates the length of the text without the substring. This number is then subtracted from the length of the original text. The result is the number of characters that were removed by SUBSTITUTE.
Then, the number of characters removed is divided by the length of the substring. So, if a substring or word is 5 characters long, and there are 10 characters missing after it’s been removed from the original text, we know the substring/word appeared twice in the original text.
Because the above calculation is wrapped in the SUMPRODUCT function, the result is an array that contains a number for each cell in the range. These numbers represent the number of occurrences of the substring in each cell. For this example, the array looks like this: {1;1;0;1}
Finally, SUMPRODUCT sums together all items in the array to get the total occurrences of substring in the range of cells.
Ignoring case
SUBSTITUTE is a case-sensitive function, so it will match case when running a substitution. If you need to count both upper and lower case occurrences of a word or substring, use the UPPER function inside SUBSTITUTE to convert the text to uppercase before running the substitution:
=SUMPRODUCT((LEN(rng)-LEN(SUBSTITUTE((UPPER(rng)),UPPER(txt),"")))/LEN(txt))
Explanation
The LEN function is fully automatic. In the example, the formula in the active cell is:
=LEN(B5)
The LEN function simply counts all characters that appear in a cell. All characters are counted, including space characters, as you can see in cell C9.
Numbers
Numbers in Excel (including dates, times, currency, etc.) are often formatted with a number format . It’s important to understand that the characters that makeup numbers are counted in their raw form (i.e. number formatting is not included). For example, because Excel dates are serial numbers , the result in cell C11 is 5, since the date in B11 is actually the number 42005.