Excel formula to count cells with certain text (exact and partial match)

The tutorial shows how to count number of cells with certain text in Excel. You will find formula examples for exact match, partial match and filtered cells.

Last week we looked at how to count cells with text in Excel, meaning all cells with any text. When analyzing large chunks of information, you may also want to know how many cells contain specific text. This tutorial explains how to do it in a simple way.

How to count cells with specific text in Excel

Microsoft Excel has a special function to conditionally count cells, the COUNTIF function. All you have to do is to supply the target text string in the criteria argument.

Here's a generic Excel formula to count number of cells containing specific text:

COUNTIF(range, "text")

The following example shows it in action. Supposing, you have a list of item IDs in A2:A10 and you want to count the number of cells with a particular id, say "AA-01". Type this string in the second argument, and you will get this simple formula:

=COUNTIF(A2:A10, "AA-01")

To enable your users to count cells with any given text without the need to modify the formula, input the text in a predefined cell, say D1, and supply the cell reference:

=COUNTIF(A2:A10, D1)
Excel formula to count cells with specific text

Note. The Excel COUNTIF function is case-insensitive, meaning it does not differentiate letter case. To treat uppercase and lowercase characters differently, use this case-sensitive formula.

How to count cells with certain text (partial match)

The formula discussed in the previous example matches the criteria exactly. If there is at least one different character in a cell, for instance an extra space in the end, that won't be an exact match and such a cell won't be counted.

To find the number of cells that contain certain text as part of their contents, use wildcard characters in your criteria, namely an asterisk (*) that represents any sequence or characters. Depending on your goal, a formula can look like one of the following.

Count cells that contain specific text at the very start:

COUNTIF(range, "text*")

Count cells that contain certain text in any position:

COUNTIF(range, "*text*")

For example, to find how many cells in the range A2:A10 begin with "AA", use this formula:

=COUNTIF(A2:A10, "AA*")

To get the count of cells containing "AA" in any position, use this one:

=COUNTIF(A2:A10, "*AA*")

To make the formulas more dynamic, replace the hardcoded strings with cell references.

To count cells that begin with certain text:

=COUNTIF(A2:A10, D1&"*")

To count cells with certain text anywhere in them:

=COUNTIF(A2:A10, "*"&D1&"*")

The screenshot below shows the results:
Formula to count cells containing a given text string

Count cells that contain specific text (case-sensitive)

In situation when you need to differentiate uppercase and lowercase characters, the COUNTIF function won't work. Depending on whether you are looking for an exact or partial match, you will have to build a different formula.

Case-sensitive formula to count cells with specific text (exact match)

To count the number of cells with certain text recognizing the text case, we will use a combination of the SUMPRODUCT and EXACT functions:

SUMPRODUCT(--EXACT("text", range))

How this formula works:

  • EXACT compares each cell in the range against the sample text and returns an array of TRUE and FALSE values, TRUE representing exact matches and FALSE all other cells. A double hyphen (called a double unary) coerces TRUE and FALSE into 1's and 0's.
  • SUMPRODUCT sums all the elements of the array. That sum is the number of 1's, which is the number of matches.

For example, to get the number of cells in A2:A10 that contain the text in D1 and handle uppercase and lowercase as different characters, use this formula:

=SUMPRODUCT(--EXACT(D1, A2:A10))
Case-sensitive formula to get the number of cells with particular text

Case-sensitive formula to count cells with specific text (partial match)

To build a case-sensitive formula that can find a text string of interest anywhere in a cell, we are using 3 different functions:

SUMPRODUCT(--(ISNUMBER(FIND("text", range))))

How this formula works:

  • The case-sensitive FIND function searches for the target text in each cell of the range. If it succeeds, the function returns the position of the first character, otherwise the #VALUE! error. For the sake of clarity, we do not need to know the exact position, any number (as opposed to error) means that the cell contains the target text.
  • The ISNUMBER function handles the array of numbers and errors returned by FIND and converts the numbers to TRUE and anything else to FALSE. A double unary (--) coerces the logical values into ones and zeros.
  • SUMPRODUCT sums the array of 1's and 0's and returns the count of cells that contain the specified text as part of their contents.

To test the formula on real-life data, let's find how many cells in A2:A10 contain the substring input in D1:

=SUMPRODUCT(--(ISNUMBER(FIND(D1, A2:A10))))

And this returns a count of 3 (cells A2, A3 and A6):
Case-sensitive formula to count cells containing certain text anywhere in them

How to count filtered cells with specific text

To count visible items in a filtered list, you will need to use a combination of 4 or more functions depending on whether you want an exact or partial match. To make the examples easier to follow, let's take a quick look at the source data first.

Assuming, you have a table with Order IDs in column B and Quantity in column C like shown in the image below. For the moment, you are interested only in quantities greater than 1 and you filtered your table accordingly. The question is – how do you count filtered cells with a particular id?
How to count filtered cells with certain text

Formula to count filtered cells with specific text (exact match)

To count filtered cells whose contents match the sample text string exactly, use one of the following formulas:

=SUMPRODUCT(SUBTOTAL(103, INDIRECT("A"&ROW(A2:A10))), --(B2:B10=F1))

=SUMPRODUCT(SUBTOTAL(103, OFFSET(A2:A10, ROW(A2:A10) - MIN(ROW(A2:A10)),,1)), --(B2:B10=F1))

Where F1 is the sample text and B2:B10 are the cells to count.
Formula to count filtered cells with particular text

How these formulas work:

At the core of both formulas, you perform 2 checks:

  1. Identify visible and hidden rows. For this, you use the SUBTOTAL function with the function_num argument set to 103. To supply all the individual cell references to SUBTOTAL, utilize either INDIRECT (in the first formula) or a combination of OFFSET, ROW and MIN (in the second formula). Since we aim to locate visible and hidden rows, it does not really matter which column to reference (A in our example). The result of this operation is an array of 1's and 0's where ones represent visible rows and zeros - hidden rows.
  2. Find cells containing given text. For this, compare the sample text (F1) against the range of cells (B2:B10). The result of this operation is an array of TRUE and FALSE values, which are coerced to 1's and 0's with the help of the double unary operator.

Finally, the SUMPRODUCT function multiplies the elements of the two arrays in the same positions, and then sums the resulting array. Because multiplying by zero gives zero, only the cells that have 1 in both arrays have 1 in the final array. The sum of 1's is the number of filtered cells that contain the specified text.

Formula to count filtered cells with specific text (partial match)

To count filtered cells containing certain text as part of the cell contents, modify the above formulas in the following way. Instead of comparing the sample text against the range of cells, search for the target text by using ISNUMBER and FIND as explained in one of the previous examples:

=SUMPRODUCT(SUBTOTAL(103, INDIRECT("A"&ROW(A2:A10))), --(ISNUMBER(FIND(F1, B2:B10))))

=SUMPRODUCT(SUBTOTAL(103, OFFSET(A2:A10, ROW(A2:A10) - MIN(ROW(A2:A10)),,1)), --(ISNUMBER(FIND(F1, B2:B10))))

As the result, the formulas will locate a given text string in any position in a cell:
Formula to count filtered cells with a certain text string in any position

Note. The SUBTOTAL function with 103 in the function_num argument, identifies all hidden cells, filtered out and hidden manually. As the result, the above formulas count only visible cells regardless of how invisible cells were hidden. To exclude only filtered out cells but include the ones hidden manually, use 3 for function_num.

That's how to count the number of cells with certain text in Excel. I thank you for reading and hope to see you on our blog next week!

Available downloads

Excel formulas to count cells with certain text

144 comments

  1. This is a very informative tutorial. I have tried to create a Power Query DAX measure to do the the same, but I cannot do it. Using the Countif formulae in Excel is much easier. Thanks.

  2. Hello!

    Hope you can help me with the following.

    I have column A with names and B with cost. Different companies sent me their budgets and I want a formula that tells me how much the cost they told me.

    Example:

    I'm doing a Tab where I put which product and cost per company they budgeted, as not every company offer the same fruits

    Company XX (merged columns A and B) Company YY (merged columns C and D)

    Column A Column B Column C Column D
    Banana 10$ Melon 15$
    Apple 20$ Banana 12$
    Melon 15$

    and in another tab I want the totals and say Company XX if they sell bananas, give me the result on the cell next to it, if they don't sell them (if blank) write not bidded. So this new tab will like like

    XX YY
    Banana 10$ 12$
    Apple 20$ not bidded
    Melon 15$ 15$

    I'm trying countif(range:range,banana, BUT I dont know how to say "give me the number in cell next to it and if is blank say "not bidded"

    Thank you!!!

  3. Hi,
    How to count cells with certain numeric value?
    For example this is a column A
    12
    13
    21
    And I want to find how many "1" counts?
    =Countif(a:a,"*1*")
    This returns 0 instead of 3
    Can you help me
    Thanks

  4. Dear Alexander,

    I have set up a Google Form linked to a sheet to register visitors to the school where I work.

    I'm using conditional formatting with a custom formula "=COUNTIF(B:B,B:B)>1" to apply a rule for instances of names that occur more than once in the column. In this case, it will grey out visitors' names who have completed their visit, leaving only current visitors in regular formatting.

    But in the case where a visitor returns for a another visit, their name will appear three (or more times), and therefore will also be greyed out.

    How do I modify the formula to only apply to counts that are even numbers, rather than just greater than one?

    Thank you!

  5. Hi,
    Below is the formula I am using.

    =IF(COUNTIF(V4:Y10,"*Issue*"),"PASS","FAIL")

    Problem is.... no matter what I do it will give me PASS if its the first option and give me FAIL if I move it to the first option like shown below.

    =IF(COUNTIF(V4:Y10,"*Issue*"),"FAIL","PASS")

    There is not an "issue" in my range I do not understand why this is not working, but it is on my other sheets.

    Anyone able to help me solve this?

    Thanks!

  6. I can't seem to find an answer to a very simple problem: I want to COUNTIF cells whose value does not equal a string, but that string has a space in it.
    I have tried the following permutations and they don't work

    =COUNTIFS(A1:A4,"NOT FOUND")
    =COUNTIFS(A1:A4,"NOT FOUND")
    =COUNTIFS(A1:A4,""NOT FOUND"")
    =COUNTIFS(A1:A4,"'NOT FOUND'")

    Any help much appreciated!

  7. Looking for a formula that counts specific text in a row. For example in a row of 31 characters how mant times does "V" appear?

  8. Good afternoon.

    I'm trying to figure out a way to count how many times a certain name appears in a spreadsheet, with a "yes" in the cell next to it.
    Is that possible?

    Thanks

  9. =COUNTIF(MID($C$2:C6,2,4),MID($C$2:C6,2,4))

    Hi, is there a way to workaround this error without creating another column to extract the texts first? Need a counter for every unique set of strings in a cell's value without the first and last characters, such that E12231 and N12230 will count as one since "1223" is the same. Is there an alternative to the MID function that can work with ranges?

  10. =COUNTIF(MID($C$2:C6,2,4),MID($C$2:C6,2,4))

    Hi, is there a way to workaround this error without creating another column to extract the texts first? Need a counter for every unique set of strings in a cell's value without the first and last characters, such that E12231 and N12230 will count as one since "1223" is the same. Is there an alternative to the MID function that can work with ranges?

  11. I am attempting to use the COUNTIF but I have issue with names. I have 2 staff; Val and Valentina. I want to count for both but the formula with "Val" is also adding in the "Valentina" information. How do I keep "Val" from adding Valentina's info??? =COUNTIF(O:O,"Val") but it also includes Valentina. How do I add a NOT Valentina to this count cell???

  12. Hello, I am looking to count if text contains key words

    I need the formula to check for cells that contain "abdo" anywhere in the box and "cramp"

    I want this to be able to pick up; crampy abdominal pain, abdominal cramps, abdominal cramping etc

    =COUNTIF(G2:G151,"*"&"cramp"&"*"&"*"&"abdo"&"*")

    This formula gets only some of the data and I'm not sure how to make it include all of it!

    Thanks

      • Thanks for your reply

        I'm afraid that returned no results and there are around 12 comments with both pieces of information.

          • Excellent! That does indeed work!

            =SUMPRODUCT(--ISNUMBER(SEARCH("Small",Sheet1!J2:J145)),--ISNUMBER(SEARCH("bowel",Sheet1!J2:J145)),--ISNUMBER(SEARCH("dist*",Sheet1!J2:J145)*OR("Dila*",Sheet1!J2:J145)))

            Here I'm trying to search for the boxes with EITHER small bowel distention OR small bowel dilation.

            It does not work, any ideas?

            Also, thanks for all your help! It has been, for want of a better word, helpful.

  13. Hi, this article is linked to the COUNTIF article, in which you can count the number of times items in a list are seen in another list. However, as the article states, COUNTIF is case insensitive, and directs you here for case sensitive.
    This article does not provide an alternative method that is case sensitive. Is it possible?

  14. I am trying to to count only cells that are visible in a sorted list. For example, my current formula is =COUNTIF($D$358:$D$95320,A95327). The text in A95327 is EXECUTIVE, however it is counting all EXECUTIVE in the D Column, even the the cells that are hidden due to sorting. Anyone have a solution? Thank you so much.

  15. **** UPDATED COMMENT WITH CORRECT FORMULA ******

    Hi There,

    I am trying to come up with the part of the formula that would allow me to exclude partial match of multiple strings in one. However, the part with "*" does not work for multiple cell selection, as seen in the formula below. Is there alternative efficient way of doing it? The only other way I could think of is to exclude each string individually, but that would be the formula super ugly and long.

    Here is the formula:
    =SUM(SUMIFS('raw data'!C:C,'raw data'!V:V,"*"&'STEPS - GENERAL'!E27:E30&"*",'raw data'!A:A,"*"&'STEPS - GENERAL'!D36:D57&"*"))

    Thank you,

    Lilly

    • Hi!
      Your question is about working with the SUMIFS function.
      It is very difficult to understand a formula that contains unique references to your workbook worksheets.
      I assume that the problem is that you are using ranges of different values for the criteria - E27:E30 and D36:D57. From the range D36:D57, only the first 3 values are used: D36, D37, D38. The criteria ranges must be the same size.

      • Hi Alexander,

        Thanks for a prompt response.

        I am not sure why, but the comment removes angles brackets for some reason?

        You are right, I should have created a sample formula instead. I'll try again and hopefully the angle brackets will appear this time.

        Here is what I am trying to do (sample formula):

        Sum the numbers from the column A if the column B contains a partial match of Cells Y1:Y10 and column B does NOT contain partial match of cells Z1:Z30.

        I believe the formula should be the following:
        =SUM(SUMIFS(A:A,B:B,"*"&Y1:Y10&"*",B:B,"*"&Z1:Z30"*"))

        My issue is with the part about does NOT contain as it doesn't. The only way it does work is when I exclude the cells separately =SUM(SUMIFS(A:A,B:B,"*"&Y1:Y10&"*",B:B,"*"&Z1"*",B:B,"*"&Z2"*",B:B,"*"&Z3"*"........))

        This way of course the formula becomes super long.

        So I was wondering if there is another way of doing the part of does NOT contain partial match for multiple cells in bulk?

        Thank you,

        Lilija

        • Right, so the angle brackets do disappear every time I post a comment. Very weird! The only way is to spell them out in the formula instead then.

          My original formula that doesn't work:
          =SUM(SUMIFS(A:A,B:B,"*"&Y1:Y10&"*",B:B,"angle brackets*"&Z1:Z30&"*"))

          The only way it works is when the cells are excluded separately:
          =SUM(SUMIFS(A:A,B:B,"*"&Y1:Y10&"*",B:B,"angle brackets*"&Z1&"*",B:B,"angle brackets*"&Z2&"*",B:B,"angle brackets*"&Z3&"*"........))

          I would appreciate any help on this one.

          Thank you,

          Lilija

        • Right, so the angle brackets do disappear when I post a comment. Very weird! I think the only way for me to show the full formula is to spell the angle brackets instead:

          The formula that doesn't work is the following:

          =SUM(SUMIFS(A:A,B:B,"*"&Y1:Y10&"*",B:B,"angle brackets*"&Z1:Z30&"*"))

          I would appreciate any help on this one.

          Thank you,

          Lilija

  16. How can i get something similar to this work? Jack will be on different Classes "A,B,C,D,E,F" and i would like to have a count of how many A classes he has, how many B, C, D, E, F.
    formula for each letter will be used for different cells (A on A1, B on B1, C on C1....)

    =countifs(X:X=O5,I6,W:W)

    Please if anyone has the answer this would be much appreciated. I can do this using a Pivot Table, but i prefer to have the table so it refreshes automatically once a Cell is updated without me refreshing all the time.

    Thanks :)

  17. Hi,
    Thank you for the tutorial.
    I'm hoping you can help with the following task that I'm trying to achieve.
    I have a sales report where it contains sales order number and product code. I'm trying to find out how many order contains a certain set of product only. For example I have product code (A, B, C, D, E) and my data set/sales report are as follows:
    Column 1 (Sales Order#) Column2(Product Code)
    100 A
    100 B
    101 E
    101 D
    101 A
    102 B
    102 C
    103 A
    103 E
    104 A

    I would like to count how many order contains product A, B and/or C only. In the Example above I should have only 3 as the count result (Order# 100, 102 and 104). Order# 101 and 103 are eliminated due to having product code D and E.
    Appreciate your help.

  18. Hi there,

    I want to learn how to create a formula to tally a location from an enquiry. The many examples i've seen the data you want to tally is known, ie how many times is the word apple used. My data is unknown till entered & will change on a weekly basis.

    So i have the set cells i want the data tallied from being H7, H13, H19 & H25.

    If i use example data that would be inputted into these cells. H7= Liverpool, H13: Fairfield, H19: Oran Park, H25: Liverpool

    I want excel to spit out & identify that i had x2 Liverpool, x1 Fairfield & x1 Oran Park.

    How is that possible?

  19. I can trying to count total instances that match both a specific text value in a column and a second column if ISNUMBERIC. Example data might look like this.

    Month | Processed | ID
    July | not processed | NULL
    July | processed | 123
    Aug | not processed | 234
    Aug | processed | NULL
    Sept | not processed | 765

    Here is what I am trying to do in a function and if it worked would return a count of 1 as my result:

    The output is in Tab 0 and the data is in Tab 1,

    =COUNTIFS('Tab 1'!A:A,"July",'Tab 1'!C:C,ISNUMBER) <-- this doesn't work. I always get zero. ID is number so I know that is not my issue.

    So basically looking for July in column A and a numeric value in ID. If that is met then count up the instances.

  20. Hello!

    I'm trying to total my clinical hours by category. If I have the hours in column B and the category in column D, am I able to use the COUNTIF function to total these hours? So I don't want to count the actual number of cells in column D containing certain text like "aphasia," "language," "articulation," "fluency," but rather locate the columns that have those titles and count the numerical value in column B for the same row?

    I don't want to know how many cells contain the word "aphasia" - I want to total the hours I have (column B) in the rows containing "aphasia."

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)