Count specific characters in a range

An excel formula to count specific characters in a range

Related Functions

Sponsored Link

 Count specific characters in a range
=SUMPRODUCT(LEN(rng)-LEN(SUBSTITUTE(rng,txt,"")))

If you need to count specific characters in a range of cells, you can do so with a formula that uses LEN and SUBSTITUTE, along with the SUMPRODUCT function. In the generic form of the formula (above), rng represents a range of cells that contain words and txt represents the character you need to count.

In the example, the active cell contains this formula:

 
=SUMPRODUCT(LEN(B3:B7)-LEN(SUBSTITUTE(B3:B7,"o","")))

How the formula works

For each cell in the range, SUBSTITUTE removes all the o's from the text, then LEN calculates the length of the text without o's. This number is then subtracted from the length of the text with o's.

Because we are using SUMPRODUCT, the result of all this calculation is a list of items (an array), where there is one item per cell in the range, and each item a number based on the calculation described above. In other words, we have a list of character counts, with one character count per cell.

SUMPRODUCT then sums the numbers in this list and returns a total for all cells in the range.

SUBSTITUTE is a case-sensitive function, so it will match case when performing a substitution. If you need to count both upper and lower case occurrences of a specific character, use the UPPER function inside SUBSTITUTE to convert the text to uppercase before the substitution occurs. Then supply an uppercase character for the text that's being counted.

The modified generic form of the formula looks like this:

 
=SUMPRODUCT(LEN(rng)-LEN(SUBSTITUTE(UPPER(rng),TXT,"")))

Sponsored Link

0 votes. 0 / 5