Excel COUNTIF and COUNTIFS with OR logic

The tutorial explains how to use Excel's COUNTIF and COUNTIFS functions to count cells with multiple OR conditions, e.g. if a cell contains X, Y or Z.

As everyone knows, Excel COUNTIF function is designed to count cells based on just one criterion while COUNTIFS evaluates multiple criteria with AND logic. But what if your task requires OR logic - when several conditions are provided, any one can match to be included in the count?

There are a few possible solutions to this task, and this tutorial will cover them all in full detail. The examples imply that you have a sound knowledge of the syntax and general uses of both functions. If not, you may want to begin with revising the basics:

Excel COUNTIF function - counts cells with one criteria.

Excel COUNTIFS function - counts cells with multiple AND criteria.

Now that everyone is on the same page, let's dive in:

Count cells with OR conditions in Excel

This section covers the simplest scenario - counting cells that meet any (at least one) of the specified conditions.

Formula 1. COUNTIF + COUNTIF

The easiest way to count cells that have one value or another (Countif a or b) is to write a regular COUNTIF formula to count each item individually, and then add the results:

COUNTIF(range, criterion1) + COUNTIF(range, criterion2)

As an example, let's find out how many cells in column A contain either "apples" or "bananas":

=COUNTIF(A:A, "apples") + COUNTIF(A:A, "bananas")

In real-life worksheets, it is a good practice to operate on ranges rather than entire columns for the formula to work faster. To spare the trouble of updating your formula every time the conditions change, type the items of interest in predefined cells, say F1 and G1, and reference those cells. For example:

=COUNTIF(A2:A10, F1) + COUNTIF(A2:A10, G1)
Count cells that have one value or another.

This technique works fine for a couple of criteria, but adding three or more COUNTIF functions together would make the formula too cumbersome. In this case, you'd better stick with one of the following alternatives.

Formula 2. COUNTIF with array constant

Here's a more compact version of the SUMIF with OR conditions formula in Excel:

SUM(COUNTIF(range, {criterion1, criterion2, criterion3, …}))

The formula is constructed in this way:

First, you package all the conditions in an array constant - individual items separated by commas and the array enclosed in curly braces like {"apples", "bananas', "lemons"}.

Then, you include the array constant in the criteria argument of a normal COUNTIF formula: COUNTIF(A2:A10, {"apples","bananas","lemons"})

Finally, warp the COUNTIF formula in the SUM function. It is necessary because COUNTIF will return 3 individual counts for "apples", "bananas" and "lemons", and you need to add those counts together.

Our complete formula goes as follows:

=SUM(COUNTIF(A2:A10,{"apples","bananas","lemons"}))
COUNTIF with an array constant to count cells with OR logic

If you'd rather supply your criteria as range references, you'll need to enter the formula with Ctrl + Shift + Enter to make it an array formula. For example:

=SUM(COUNTIF(A2:A10,F1:H1))

Please notice the curly braces in the screenshot below - it is the most evident indication of an array formula in Excel:
Array formula to sum cells based on criteria as range references.

Formula 3. SUMPRODUCT

Another way to count cells with OR logic in Excel is to use the SUMPRODUCT function in this way:

SUMPRODUCT(1*(range ={criterion1, criterion2, criterion3, …}))

To better visualize the logic, this could also be written as:

SUMPRODUCT((range=criterion1) + (range=criterion2) + …)

The formula tests each cell in the range against each criterion and returns TRUE if the criterion is met, FALSE otherwise. As an intermediate result, you get a few arrays of TRUE and FALSE values (the number of arrays equals the number of your criteria). Then, the array elements in the same position are added together, i.e. the first elements in all the arrays, the second elements, and so on. The addition operation converts the logical values to numbers, so you end up with one array of 1's (one of the criteria matches) and 0's (none of the criteria matches). Because all the criteria are tested against the same cells, there is no way any other number could appear in the resulting array - only one initial array can have TRUE in a specific position, others will have FALSE. Finally, SUMPRODUCT adds up the elements of the resulting array, and you get the desired count.

The first formula works in a similar manner, with the difference that it returns one 2-dimentional array of TRUE and FALSE values, which you multiply by 1 to convert the logical values to 1 and 0, respectively.

Applied to our sample data set, the formulas take the following shape:

=SUMPRODUCT(1*(A2:A10={"apples","bananas","lemons"}))

Or

=SUMPRODUCT((A2:A10="apples") + (A2:A10="bananas") + (A2:A10="lemons"))

Replace the hardcoded array constant with a range reference, and you will get even a more elegant solution:

=SUMPRODUCT(1*( A2:A10=F1:H1))
SUMPRODUCT formula to count cells with OR logic

Note. The SUMPRODUCT function is slower than COUNTIF, which is why this formula is best to be used on relatively small data sets.

Count cells with OR as well as AND logic

When working with large data sets that have multi-level and cross-level relations between elements, chances are that you will need to count cells with OR and AND conditions at a time.

As an example, let's get a count of "apples", "bananas" and "lemons" that are "delivered". How do we do that? For starters, let's translate our conditions into Excel's language:

  • Column A: "apples" or "bananas" or "lemons"
  • Column C: "delivered"

Looking from another angle, we need to count rows with "apples and delivered" OR "bananas and delivered" OR "lemons and delivered". Put this way, the task boils down to counting cells with 3 OR conditions - exactly what we did in the previous section! The only difference is that you'll utilize COUNTIFS instead of COUNTIF to evaluate the AND criterion within each OR condition.

Formula 1. COUNTIFS + COUNTIFS

It is the longest formula, which is the easiest to write :)

=COUNTIFS(A2:A10, "apples", C2:C10, "delivered") + COUNTIFS(A2:A10, "bananas", C2:C10, "delivered")) + COUNTIFS(A2:A10, "lemons", C2:C10, "delivered"))

The screenshot below shows the same formula with cells references:

=COUNTIFS(A2:A10, K1, C2:C10, K2) + COUNTIFS(A2:A10, L1, C2:C10, K2) + COUNTIFS(A2:A10, M1,C2:C10, K2)
Add two or more COUNTIFS to count cells with OR as well as AND logic.

Formula 2. COUNTIFS with array constant

A more compact COUNTIFS formula with AND/OR logic can be created by packaging OR criteria in an array constant:

=SUM(COUNTIFS(A2:A10, {"apples","bananas","lemons"}, C2:C10, "delivered"))

When using a range reference for the criteria, you need an array formula, completed by pressing Ctrl + Shift + Enter:

=SUM(COUNTIFS(A2:A10,F1:H1,C2:C10,F2))
COUNTIFS with an array constant to count cells with AND/OR logic

Tip. If needed, you are free to use wildcards in the criteria of any formulas discussed above. For example, to count all sorts of bananas such as "green bananas" or "goldfinger bananas" you can use this formula:

=SUM(COUNTIFS(A2:A10, {"apples","*bananas*","lemons"}, C2:C10, "delivered"))

In a similar manner, you can build a formula to count cells based on other criteria types. For example, to get a count of "apples" or "bananas" or "lemons" that are "delivered" and the amount is greater than 200, add one more criteria range/criteria pair to COUNTIFS:

=SUM(COUNTIFS(A2:A10, {"apples","*bananas*","lemons"}, C2:C10, "delivered", B2:B10, ">200"))

Or, use this array formula (entered via Ctrl + Shift + Enter):

=SUM(COUNTIFS(A2:A10,F1:H1,C2:C10,F2, B2:B10, ">"&F3))
Count cells based on three AND/OR criteria of different types.

Count cells with multiple OR conditions

In the previous example, you have learned how to test one set of OR conditions. But what if you have two or more sets and you are looking to get a total of all possible OR relations?

Depending on how many conditions you need to handle, you can use either COUNTIFS with an array constant or SUMPRODUCT with ISNUMBER MATCH. The former is relatively easy to build, but it is limited to only 2 sets of OR conditions. The latter can evaluate any number of conditions (a reasonable number, of course, given Excel's limit to 255 arguments and 8192 characters to the total formula length), but it may take some effort to grasp the formula's logic.

Count cells with 2 sets of OR conditions

When dealing with only two sets of OR criteria, just add one more array constant to the COUNTIFS formula discussed above.

For the formula to work, one minute but critical change is needed: use a horizontal array (elements separated by commas) for one criteria set and vertical array (elements separated by semicolons) for the other. This tells Excel to "pair" or "cross-calculate" the elements in the two arrays, and return a two-dimensional array of the results.

As an example, let's count "apples", "bananas" or "lemons" that are either "delivered" or "in transit":

=SUM(COUNTIFS(A2:A10, {"apples", "bananas", "lemons"}, B2:B10, {"delivered"; "in transit"}))

Please note the semicolon in the second array constant:
Count cells with 2 sets of OR conditions.

Because Excel is a 2-dimentional program, it is not possible to construct a 3-dimentional or 4-dimentuional array, and therefore this formula only works for two sets of OR criteria. To count with more criteria, you will have to switch to a more complex SUMPRODUCT formula explained in the next example.

Count cells with multiple sets of OR conditions

To count cells with more than two sets of OR criteria, use the SUMPRODUCT function together with ISNUMBER MATCH.

For example, let's get a count of "apples", "bananas" or "lemons" that are either "delivered" or "in transit" and are packaged in either "bag" or "tray":

=SUMPRODUCT(ISNUMBER(MATCH(A2:A10,{"apples","bananas","lemons"},0))*
ISNUMBER(MATCH(B2:B10,{"bag","tray"},0))*
ISNUMBER(MATCH(C2:C10,{"delivered","in transit"},0)))

In the heart of the formula, the MATCH function checks the criteria by comparing each cell in the specified range with the corresponding array constant. If the match is found, it returns a relative position of the value if the array, N/A otherwise. ISNUMBER converts these values to TRUE and FALSE, which equate to 1 and 0, respectively. SUMPRODUCT takes it from there, and multiplies the arrays' elements. Because multiplying by zero gives zero, only the cells that have 1 in all the arrays survive and get summed.

Th screenshot below shows the result:
Count cells with multiple sets of OR conditions.

This is how you use the COUNTIF and COUNTIFS functions in Excel to count cells with multiple AND as well as OR conditions. To have a closer look at the formulas discussed in this tutorial, you are welcome to download our sample workbook below. I thank you for reading and hope to see you on our blog next week!

Practice workbook

Excel COUNTIF with OR conditions - examples (.xlsx file)

172 comments

  1. EX 1,2,3 this following text , we need to sepatate

    1,2,3
    2,1,3
    3,1,2
    3,2,1
    2,3,1
    1,3,2 - This formet - How to I do this

  2. Lovely article. U have really covered many examples..

  3. Hi,
    MultiPul times how to match that time one to other - please tell me there any time relation matching formulas

    A B C D E
    6:59:59 AM 07:00:12 07:00:15 07:00:34 07:00:36
    07:00:36 07:00:45 07:00:48 07:01:51 07:01:53
    07:01:53 07:02:00 07:02:03 07:02:37 07:02:39
    07:02:39 07:02:50 07:02:53 07:03:23 07:03:25
    07:03:25 07:03:41 07:03:44 07:04:20 07:04:22
    07:04:22 07:04:34 07:04:37 07:04:58 07:05:00
    07:05:00 07:05:07 07:05:10 07:05:43 07:05:45
    07:05:45 07:05:57 07:06:00 07:06:24 07:06:26
    07:06:26 07:06:35 07:06:38 07:06:56 07:06:58

  4. How to use countif function one range multiple criteria

  5. Hello Alexander,

    Thank you so much for your GREAT tutorial.
    This really really help my work life more easier, especially for "COUNTIF with array constant"

    The formulation is workable for several wording criteria. But it's not workable when I combined different type of criteria, which is date & wording.
    Such as, this criteria ">"&TODAY(). I would like to count items which included status "date after today" or "TBC".

    Could you please help to sharing solution for make this workable "=SUM(COUNTIFS($I:$I,{">"&TODAY(),"TBC"}))?

    Thank you in advance for your great suggestion :)

  6. A couple questions:

    1. When applying multiple 'OR' criteria to a range, can one supply both text and cell reference criteria in a compact format, i.e., something like:

    =SUM(COUNTIF(A2:A10,{$F$1,"bananas","lemons"}))
    =SUM(COUNTIF(A2:A10,{&$F$1,"bananas","lemons"}))

    My Excel 2010 doesn't like these formulas. Is there another way using countif to apply multiply OR criteria to a range without adding multiple countifs together using +?

    2. Does Excel allow one to use functions within criteria in countif? Specifically, I would like to use not(), isblank(), left(), etc. I am thinking something like:

    =SUM(COUNTIF(A2:A10,not($F$1)))
    =SUM(COUNTIF(A2:A10,left($F$1,6)))

    Thanks.

  7. I am trying to get my excel sheet to read ab=30 minutes and for the row to be read and automatically apply if there are 4 abs in the entire row, that it would show total time of 120 minutes. If that makes sense.

    • Hi! Sorry, I have no idea exactly what the task is. Please describe your problem in more detail. Write an example of the source data and the result you want to get.

  8. I am trying to check three columns (say, A, B and C) for a particular region and, if the region it appears in any of the columns, then it should check a fourth column (say D) for another criteria (outcome) and count how many rows meet the outcome criteria. That would be simple if a particular region only appeared in column A or B or C. The problem I have is that the region may appear in one row in both columns A and B but I don't want it to count that as two (i.e. it must only count the region once if it appears in any of the three region columns AND meets the outcome). But if it appears in only column B and meets the outcome criteria, then it also only counts once. I've tried many ways to do this and just cannot seem to get it to work but I know it must be do-able.

  9. I need formula for differentiating duplicate values in column with integer in the end. For example, if there are 3 duplicate values in column i.e. "Apple" and I want to differentiate word "Apple" with "Apple-1", "Apple-2", "Apple-3" in next column.

  10. I need a formula that shows issues that were opened after one date and closed before a second , OR are still open now.

    • Hi! If you want to show issues, why are you asking a question in an article that describes counting by condition? To show issues by condition, use the FILTER function. You can find the examples and detailed instructions here: Filter with multiple AND criteria. I can't recommend a formula to you as I can't see your data.

  11. I have a spread sheet with two columns that I want to COUNTIF with if an OR statement is true.

    The two columns (Call them A and B) both have optional values of "Y", "N", or blank. I want to set up a count of the number of rows where either column's value is "Y". Also I would like to display that as a percentage of 'Not Blank' (i.e. anything in column A or B)

    For Example if A1,2 and 3 are "Y", B3 & 4 are "Y", B5 is "N" and A&B 6 are blank then the count returns "4" and the percentage "80%"

  12. I have a monthly spreadsheet with classes from various teachers, in three periods of the day and in various classes. I'm trying to use conditional formatting to color the teacher's name red, in case I mistakenly put that teacher in different classes but on the same day and at the same time of day. I can do it for one day, but I would like to know if there would be a way to do it for days 1 to 31 in a single rule, or would I have to do each rule for one day?

    I'm using the rule:
    =COUNTIFS($B$2:$B$11;$B2;$C$2:$C$11;$C2)>1
    Ex:
    classes days 1 2 3 ...
    class 1 morning John Mary Peter ...
    class 2 afternoon Peter Mary Peter ...
    class 3 night Mary John Paul ...
    class 4 morning Mary Peter Mary ...
    class 5 afternoon John Mary John ...
    class 6 night Paul Mary Mary ...
    class 7 morning Peter John John ...
    class 8 afternoon Mary John Paul ...
    class 9 night John Paul Peter ...
    class 10 morning John Paul Peter ...

    • classes days -> 1 2 ... 31
      class1 morning John Mary Peter ...
      class2 afternoon Peter Mary Peter ...
      class3 night Mary John Paul ...
      class4 morning Mary Peter Mary ...
      class5 afternoon John Mary John ...
      class6 night Paul Mary Mary ...
      class7 morning Peter John John ...
      class8 afternoon Mary John Paul ...
      class9 night John Paul Peter ...
      class10 morning John Paul Peter ...

  13. Hi if I have an array that could contain three different YouTube versions in any of the cells, and I want to count how many cells contain ANY version of YouTube (there are 3 different versions), my COUNTIF function isn't working accurately. It's telling me that 65 cells have it, but on hand/manual count I know I have only 61 cells that have it. The issue is, some cells have multiple versions of YouTube in the same cell, and it's aggregating them all instead of just counting that cell ONCE. Any idea how fix? Please and thank you.

    • I think I understand your problem, as my problem was similar. After a lot of searching around, I've devised a workaround that is this:

      Add a column in your dataset that is set to either 0 or 1 based on whether any of the three different versions of youtube exist. Do this with an IF statement with an OR in the logical test part of the IF statement.

      Then base your COUNTIF criteria on that column, not the column containing the YouTube information itself.

      I hope that makes sense and is helpful.

  14. Hello, I am creating a tracker to account for number of tickets purchased based by pay grade in this example E8 is the paygrade. I am also trying to account for number of tickets purchased by that paygrade as their guest and I am completely lost. I don't know what to put in the ??????? spot or if this is even the correct formula.

    Example, John's paygrade is E8 who purchased 1 ticket for himself and 2 guest tickets, Michael's paygrade if E8 who purchased 1 ticket for himself and 3 guest tickets. I want to produce one cell that will count total number of E8's who purchased a ticket and the cell next to it I want to count total number of guest tickets purchased by all E8's. So one cell should have (2) and the cell adjacent should have (5) for this example.

  15. if columns B1,2,4 are Y and B,3 is N, what formula will give me a % of meetings attended for a 12 month period

  16. I have a spreadsheet for degree summaries. For courses with prereqs I want it the add the prereq courses only if students have an A, B, or C. A couple courses have four prereqs. Currently, it's counting if a prereq is not blank, and then I have to double-check the grade, which can lead to errors. Is this possible?

    • You can specify the prereqs with "countifs" rather than "count". countifs, counts values with a condition to it. So for example, if you need a person with greater than 80 grades, in countif you select the range first then give it a condition separated by a comma.

  17. I have a spreadsheet where I am using the following countifs formulas: =COUNTIFS(LOG!$H$2:$H$1249,"*Initial*",LOG!$K$2:$K$1249,"Anderson*")
    I also need to add in one more item to consider: how may of those were received in a certain month. How would I do that?

  18. Hi,

    How can I use the Countif Function in an Array ? Say I have a 2-column data Column A has the days , viz Wed, Mon, Tue, Sat, Thu, Fri, Tue, Mon, Sat, ..... and Column B has a corresponding positive or negative number. I want to find out how many positive numbers are there in Column B when I select the Column A filter = "Tue".

    Thanks

    • To calculate the number of values for the two conditions, use the COUNTIFS function. All the information you need is in the article above. Here is an example of a formula:

      =COUNTIFS(A1:A10,"Tue",B1:B10,">0")

      • Thanks for the input. But this formula would be on a case to case basis. But when I am selecting a " Tue" or a "Wed" from the dropdown filter in Column A or multi select "Tue" &"Wed", I would like to find out the no. of counts of positive numbers in Column B for those Days which have been selected via the Drop Down filter of Column A.

        Regards

        • Hi! The filter in the column has no effect on the results of the formula calculation. You need to add OR logic as described in the article above. For example,

          =COUNTIFS(A1:A10,"Tue",B1:B10,">0") + COUNTIFS(A1:A10,"Wed",B1:B10,">0")

          • Actually , I am looking for a formula that will count the positive numbers based on the filter of Column A. It has to be an array formula.

  19. Hello! Thank you for all the tutorials. They are ALL very helpful.

    You might have covered this before, but I could not find it.

    How can I count if more than one cell in a range contains one criterion (Yes)? I need to count how many people selected more than one race. (6 cells, how many are marked 'Yes')

    Once again, thank you!
    - Dánae

  20. "Hello,
    I need help with a task in a spreadsheet that involves searching a table for entries based on several criteria and counting them. Specifically, I need to find entries that meet the following conditions:

    Column G must equal 'No' AND
    Column O must be less than or equal to a date specified in cell C22 AND
    Columns U, AK, and AL must either be equal to 0 OR greater than the date in cell C22

    I would greatly appreciate any assistance or guidance on how to accomplish this task using Excel. Thank you in advance for your help!"

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