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:

  • Step 1: LEN(B2:B12): To count the length of each cell;
  • Step 2: (LEN(B2:B12)=8): If the length equals 8, the result returns TRUE; otherwise, it returns FALSE;
  • Step 3: –(LEN(B2:B12)=8): The double hyphen (or times 1) converts TRUE into one and FALSE into zero;
  • Step 4: SEARCH("WILLIAMS", B2:B12): To search the text location. The result returns a number if the cell contains the text; otherwise, it returns FALSE;
  • Step 5: ISNUMBER(SEARCH("WILLIAMS", B2:B12)): The result returns TRUE if the cell contains the text, or FALSE if not;
  • Step 6: –ISNUMBER(SEARCH("WILLIAMS", B2:B12)): The double hyphen (or times 1) converts TRUE into one and FALSE into zero;
  • Step 7: The SUMPRODUCT function returns the sum of the products of the new array.

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

Leave a Reply