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

You may also be interested in

56 responses to "Excel formula to count cells with certain text: exact and partial match"

  1. Laiba says:

    Fail+Pass+Pass = PASS
    Pass+Fail+Pass = PASS
    Pass+Pass+Fail = FAIL
    Fail+Pass+Pass = FAIL
    HOW ??????
    Help me to solve this.

  2. Matthew Turney says:

    Is there a countif formula to count cells if they do NOT contain specific words. For example, I have a countif formula for everything that contains "BAW" in it. Now I would like a countif formula for everything except "BAW" in it but I need to be able to address multiple words like {"BAW","BAE"} etc. Any help would be appreciated. Thank you.

  3. Lori-Ann says:

    Thank you for your tutorial it was very insightful and helpful to me. I am wondering though if there is a countif formula that will count letters and numbers and then total the numbers? For Example: if I have cells that contain V10, V8, F8, F4, and V2 is there a countif formula that would total the numbers with the V (10+8+2=20)and the F (8+4) and then total the cells so they read V20 or F10?
    I do not know if that is even possible but I am hoping that it is.
    Thank you very much for your time

    • Hello Lori-Ann!
      If I understand your task correctly, the following formula should work for you:

      =CONCATENATE("V",SUMPRODUCT(--(LEFT(A1:A16,1)="V"), --(IFERROR(RIGHT(A1:A16,LEN(A1:A16)-1),0))))

      I hope this will help, otherwise please do not hesitate to contact me anytime.

  4. Bianca Schurink says:

    Hi
    I'm looking for a formula that if column F says 'VA' then the numeric number in the same row but column E needs to be added into a total on a table.
    If the cell in column F is changed to a different variable eg 'OS' then the value is subtracted from the 'VA' total and added to the 'OS' total.

    • Hello Bianca!
      I’m sorry but your task is not entirely clear to me. For me to be able to help you better, please describe your task in more detail. Please specify what you were trying to find, what formula you used and what problem or error occurred. Give an example of the source data and the expected result.
      It’ll help me understand it better and find a solution for you. Thank you.

    • Beverley M says:

      Bianca, your best bet is to use a pivot table to sumarize data that way. But if you really want it on the same sheet, use SUMIF. E.g. in your case you might have the formula for the total of the VA values have something like =SUMIF(F1:F100,"VA",E1:E100) (assuming you have 100 rows of data)

  5. Richard McWhorter says:

    Hello,

    I like your tutorials! Very clear and direct. I am trying to sort thru a column containing ship to information. Specifically I am wanting to count the number of orders that were shipped to each state. Sometimes the Stated is spelled out, say Georgia. Other times it is abbreviated, say GA. I can use countif with a wildcard, but then I need create a formula for each state. Is there an easy way to review the range, group it by state and then count the results? Thanks in advance for any help you can provide!

  6. Chantal says:

    Hi. I have a worksheet with numerous text and numbers. In column A I have different text:A1 AAAA A2: BBBB: A3: AAAA as well. In column be I have numbers B1:1,B2:2,B3:1. How do I lookup for AAAA in column and then count the values which is picked up in column B?

  7. Marin says:

    Good afternoon,
    I have last names in cells in column c. Task is to count all cells in range that contains letter "v" on second place. Would You be so kind and give me solution.

  8. PoppyF says:

    Hi - I'm looking for a formula which will help me with the following:

    To return a value (P for example) in a cell when there is a name within the text of a cell (the text will involve other words than just the name, as it's a description) in a different column that corresponds to a name within a list in a different sheet of the workbook. Multiple names within the list which all need to be checked against what comes up in the cells I'm searching to return a value from.

    Any help will be hugely appreciated! Many thanks

  9. Jacques Boyde Panteria Paragas says:

    Im trying to create a Dashboard on Sheet 1

    To count a Specific Word (Column A) based on a Specific Day (Column B) on Sheet 2

  10. Vibha says:

    i want to count the number of cells containing either "*provis*" or "*under*". What should be the formula?

  11. Laura Wright says:

    Hello! I need to countif the value in c* = .NET AND the value of e* = No. I keep getting value errors and for the life of me can't figure out why. Thanks! This tutorial was very helpful!

    • Hello!
      Unfortunately, without seeing your data it is difficult to give you any advice. Please provide me with an example of the source data and the expected result. Please specify what formula you used and what problem or error occurred. It’ll help me understand it better and find a solution for you.

  12. Cora says:

    Hi,
    I've been trying to solve a problem for a while, but am still struggling.
    The task: I have a list of product names (column A), some of which are very similar (e.g. Protos 1, Protos 100, or Tarin 3, Tarin 3 Pro). Cells can contain more than 1 product.
    When I use the countif formula with *, I get double counts (every mention of Protos 100 is also counted for Protos 1 etc.).
    So I've built the following formula:

    =COUNTIFS(K:K,"*Protos 1*",K:K,""*Protos 100*")

    However, now I am missing results that mention both: Protos 1 AND Protos 100. So I added another part:

    =COUNTIFS(K:K,"*Protos 1*",K:K,""*Protos 100*")+COUNTIFS(K:K,"*Protos 1*",K:K,"*Protos 100*")

    The idea for the second Countifs was that I want to validate both criteria: mentions of Protos 1 AND Protos 100. But the problem now is that this part K:K,"*Protos 1*" in the second countif again gives me all of the Protos 100 as well.

    Would you have any tip on how to solve this?
    Thank you!

  13. Jatin says:

    Hi, I am trying to find a formula, which can help me count the number "ge-" or "GigabitEthernet' in a cell with these values.

    Cell 1 : "ge-1/0/13.0;ge-1/0/14.0;ge-1/0/39.0"

    Cell 2 : "GigabitEthernet1/0/2;GigabitEthernet1/0/3;GigabitEthernet1/0/4;"

  14. Andy says:

    If I have the following:
    A B

    1 G 1
    2 W 2
    3 R 2
    4 G 1
    5 R 1

    Where G, W, R indicates a colour (Green, Red or White) and the numbers in column B indicate what team you are on - what would the formula be to count for example all the R on Team 1?

    I can get a formula to count the how many overall are on W, R or G but now I need a formula to work out how many W are on Team 1, How many W are on team 2, How many R are on team 1, how many R are on team 2 etc etc....

    Thanks in advance for any help.

  15. John says:

    A1 = Name 123
    = COUNTIF(A1,"=*Name #*")

    I would like the COUNTIF to be able to count if any number after "Name" is >0. What do I replace # with in the above formula?

    • Hello!
      Sorry, it's not quite clear what you are trying to achieve. The expression “Name” is> 0 is incorrect, because text and number cannot be compared. Could you please describe it in more detail? What result do you want to get? Give an example of the source data and the expected result.

      • John says:

        A1 = Name

        B2=COUNTIF(A2,"*"&A$1&" 0*")
        +COUNTIF(A2,"*"&A$1&" 1*")
        +COUNTIF(A2,"*"&A$1&" 2*")
        +COUNTIF(A2,"*"&A$1&" 3*")
        +COUNTIF(A2,"*"&A$1&" 4*")
        +COUNTIF(A2,"*"&A$1&" 5*")
        +COUNTIF(A2,"*"&A$1&" 6*")
        +COUNTIF(A2,"*"&A$1&" 7*")
        +COUNTIF(A2,"*"&A$1&" 8*")
        +COUNTIF(A2,"*"&A$1&" 9*")

        If A2 has "Name" followed by any number (note required space in between), it will count it. For example, it will count "Name 1", "Name 22", or "Name 3A". But it will not count "Name A".

        I want to simplify this to one line, and not have to use ten COUNTIF's. However, I'm not sure how to do this. I can't find a logic argument to count cell with "Name" followed by a space and any number.

        I need to simplify this because I'm essentially copying this across >50,000 rows in column B to analyze A, and Excel is essentially freezing because the amount of code is so large copying the above across each cell.

        • Hello!
          If I understand your task correctly, the following formula should work for you:

          =SUM(--ISNUMBER( SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},A2,1)))

          I hope this will help, otherwise please do not hesitate to contact me anytime.

          • John says:

            Yes! Thank you, this works great. I also need to search text across multiple columns. If I extend this through 2 columns in row 2 (i.e., A2:B2), I notice summing it works:

            =SUM(--ISNUMBER( SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},A2,1))+
            --ISNUMBER(SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},B2,1)))

            But this doesn't work, and I'm not sure why:
            =SUM(--ISNUMBER(SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},A2:B2,1)))

            If I needed to do 20 columns (A2:T2), would I need to take 20 sums, or is there an easier way? For now I only need to do 2 columns of data, but I'm wondering if I could expand it easily to more columns.

            • Hello!
              If I got you right, the formula below will help you with your task:

              =SUM(--ISNUMBER(--MID($A$2:$T$2,LEN($A$1)+2,1)))

              I hope I answered your question. If something is still unclear, please feel free to ask.

              • John says:

                Regarding

                =SUM(--ISNUMBER(--MID($A$2:$T$2,LEN($A$1)+2,1)))

                For some reason if B2 is left empty (or does not have "Name" followed by a number), it will result in zero no matter what is in the other 9 cells in row 2.

                By the way, I like that this new suggestion does not have to list out numbers 0 to 9 in the code. I do find it strange Excel does not have a wildcard (like * and ?) for a number.

              • Hi,
                I have not been able to replicate this problem. The formula works if several cells are empty or there is no text "Name"

              • John says:

                Regarding,

                =SUM(--ISNUMBER(--MID($A$2:$T$2,LEN($A$1)+2,1)))

                I found more specifics to the issue.
                A1 = Name
                A2:T2 contains the data.
                Then above equation can't be used in any of the columns A2:T2, otherwise it will result in a zero if the data in that column does not get a hit. I even tried putting equation in another sheet, but same issue occurs. The equation needs to be put outside of columns used for data. Can this be resolved you think?

              • John says:

                For example, put the equation in A3, then you will notice if A2 is empty (or does not have hit of Name #) it will result in a zero no matter what is in the other data cells B2:T2. If you put equation in B3, the same happens for empty cell in B2, and so on.

              • Hi,
                I did what you wrote. I cannot repeat your problem. My formula is working correctly.

              • John says:

                Bad news regarding:

                =SUM(--ISNUMBER(--MID($A$2:$T$2,LEN($A$1)+2,1)))

                I just realized, having studied MID, NUM, ISNUMBER functions, that this will not work because I need to search for specific names followed by a number. In other words, this latest suggestion will not distinguish between "Bob 4" and "Dan 4", because both names have 3 characters. This means we're back to your previous nice suggestion of:

                =SUM(--ISNUMBER( SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},A2,1)))

                which works nice for data in A2, but not if data is in A2:B2 (i.e., several columns in row 2). Do you think there is a work around, other than the summing A2 and B2:

                =SUM(--ISNUMBER( SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},A2,1))+
                --ISNUMBER(SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},B2,1)))

          • John says:

            Excel is getting unhappy when I have to copy your equation across many cells

            A1 = Name
            A2:B100000 contains the data with Name (followed by number) among other random text.
            C2 has:

            =SUM(--ISNUMBER( SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},A2,1))+
            --ISNUMBER(SEARCH(A$1&" "&{1,2,3,4,5,6,7,8,9,0},B2,1)))

            which copied down to row 100000.

            My goal is to get rid of copying the above equation down 100000 rows, and essentially sum up the 1's and 0's for all 100000 rows of data. Any suggestions on how to calculate each row and then sum them all up without copying the equation down 100000 rows?

  16. Aaron Gordon says:

    Hi. Using COUNTIFS I'm counting specific cells based on 3 criteria. That's working fine however I wish to add 4th criteria of "if a month text in a cell on a line matches a master month cell" on the same sheet, it includes that item in the count. e.g. Feb-21 in a cell on a line and Feb-21 in cell L2, then its included.

  17. Alex says:

    Hello,

    I'm using the COUNTIF formula however, it does not autocorrect itself to a number once I click enter. The formula still remains when I click away. I have two spreadsheets - it's working fine on one but not the other. I've double checked the formula and it's correct

  18. Hannah A says:

    Hello,
    I am trying to format a document that calculates the different types of days off employees can take at my business. Ideally we would like to format the cells on the calendar with either a V for vacation or S for sick day. Along with the letter, I would like to have the hours used for example if an individual took 8 hours off for a sick day it would read S8. Is there anyway I can write a countif statement where I can separate the days off based on its category and calculate the hours used as well? I tried using a statement you shared with a different user but our situations seem to be too different for it to work for me as well. My end goal is to calculate the hours used each month for the different types of days off so employees can see the hours they have remaining to use. Thank you for any help or insights you may be able to provide me

  19. Saranya says:

    Hi ,

    I would like to calculate a specific text say "ABC" Which present under only particular column name.
    Eg: I want to calculate the number of text "ABC" available under the column names "BBB" & "CCC".

  20. satyam says:

    Hii,

    i have data in cell a1 Like VII,VI,VIII,XI,X,XII,IX

    How can i extract Specific Roman text from From Cell A1

  21. Eileen says:

    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 :)