Site icon ExcelNotes

How to Count Cells with a Fixed Length and a Text String

You can use the LEN and SUMPRODUCT functions to count the number of cells with a certain length and a text string.

Example: You are working with a worksheet with the first name in column A, the last name in column B, and the region in column C.

Question: How many people's last names have eight characters and "Williams"?

=SUMPRODUCT(–(LEN(B2:B12)=8), –ISNUMBER(SEARCH("WILLIAMS", B2:B12)))
=SUMPRODUCT((LEN(B2:B12)=8)*1, ISNUMBER(SEARCH("WILLIAMS", B2:B12))*1)

The result returns 3, so three people have last names with eight characters and "Williams". 

In column B, there are six last names with eight characters, and three are "Williams". Please note that the SEARCH function is not case-sensitive, so both "WILLIAMS" and "Williams" are counted.

Explanation:

To count case-sensitive text, you can change the SEARCH function with the FIND function. For example, how many people have last names with eight characters and "WILLIAMS" (upper case)?

=SUMPRODUCT(–(LEN(B2:B12)=8), –ISNUMBER(FIND("WILLIAMS", B2:B12)))
=SUMPRODUCT((LEN(B2:B12)=8)*1, ISNUMBER(FIND("WILLIAMS", B2:B12))*1)

The following are the details from step 1 to step 6:

Last NameStep 1Step 2Step 3Step 4Step 5Step 6
JOHNSON7FALSE0#VALUE!FALSE0
Jones5FALSE0#VALUE!FALSE0
BROWN8TRUE11TRUE1
JONES5FALSE0#VALUE!FALSE0
MARTINEZ8TRUE1#VALUE!FALSE0
BROWN4FALSE0#VALUE!FALSE0
WILLIAMS8TRUE11TRUE1
ANDERSON8TRUE1#VALUE!FALSE0
WILLIAMS8TRUE11TRUE1
Johnson8TRUE1#VALUE!FALSE0
SMITHs7FALSE0#VALUE!FALSE0

Exit mobile version