Note: The other languages of the website are Google-translated. Back to English

Count cells that contain either x or y in a range in Excel

To count cells that contain either one value or another, some Excel users may add two COUNTIF functions together to deal with it, however, this method counts cells with AND logic (contain both x and y) instead of OR logical (contain either x or y). To avoid double counting the criteria, this tutorial is going to show you how to apply a formula based on the SUMPRODUCT function in details to count the number of cells in a specific range that contain either x or y in Excel.


How to count cells that contain either x or y in a range in Excel

As the below screenshot shown, there is a data range B3:B9, to count the number of cells in this range that contains either “KTE” or “KTO”, please apply the below formula to get it done.

Generic Formula

=SUMPRODUCT(--((ISNUMBER(FIND("criteria1",rng)) + ISNUMBER(FIND("criteria2",rng)))>0))

Arguments

Rng (required): The range from which you want to count cells that contain either x or y.

Criteria1 (required): One string or character you want to count cells based on.

Criteria2 (required): Another string or character you want to count cells based on.

How to use this formula?

1. Select a blank cell to output the result.

2. Enter the below formula into it and press the Enter key to get the result.

=SUMPRODUCT(--((ISNUMBER(FIND(D3,B3:B9)) + ISNUMBER(FIND(D4,B3:B9)))>0))

How these formulas work?

=SUMPRODUCT(--((ISNUMBER(FIND(D3,B3:B9)) + ISNUMBER(FIND(D4,B3:B9)))>0))

  • 1. FIND(D3,B3:B9): The FIND function checks for whether the value “KTE” in D3 existing in the specified range (B3:B9), and returns an array: {1,#VALUE!,#VALUE!,#VALUE!,#VALUE!,1,#VALUE!}.
    In this array, the two 1s represent that the first and the second to last cells in the list B3:B9 containing value “KTE”, the #VALUE!s represent the value “KTE” does not find in other cells.
  • 2. ISNUMBER{1,#VALUE!, #VALUE!, #VALUE!, #VALUE!,1, #VALUE!}: The ISNUMBER function returns TURE if meeting number in the array, and returns FALSE if meeting errors, and finally returns the new array as {TURE;FALSE;FALSE;FALSE;FALSE;TURE;FALSE}.
  • 3. FIND(D4,B3:B9): This FIND function also checks for whether the value “KTO” in D4 existing in the specified range (B3:B9), and returns an array: {#VALUE!,1,#VALUE!,#VALUE!, #VALUE!,#VALUE!,#VALUE!}.
  • 4. ISNUMBER{#VALUE!,1,#VALUE!,#VALUE!, #VALUE!,#VALUE!,#VALUE!}: The ISNUMBER function returns TURE if meeting number in the array, and returns FALSE if meeting errors, and finally returns the new array as {FALSE;TURE;FALSE;FALSE;FALSE;FALSE;FALSE}.
  • 5. {TURE;FALSE;FALSE;FALSE;FALSE;TURE;FALSE} + {FALSE;TURE;FALSE;FALSE;FALSE;FALSE;FALSE}: Here the two arrays returns the result as {1;1;0;0;0;1;0}.
  • 6. {1;1;0;0;0;1;0}>0: Here compares whether each number in the array is greater than 0 and returns the result as {TURE;TRUE;FALSE;FALSE;FALSE;TURE;FALSE}.
  • 7. --({TURE;TRUE;FALSE;FALSE;FALSE;TURE;FALSE}): These two minus signs convert “TRUE” into 1 and convert “FALSE” into 0. Here you will get a new array as {1;1;0;0;0;1;0}.
  • 8. =SUMPRODUCT({1;1;0;0;0;1;0}): The SUMPRODUCT function sums all number in the array and returns the final result as 3 in this case.

Related functions

Excel SUMPRODUCT function
The Excel COUNTBLANK function can be used to multiply two or more columns or arrays together, and then get the sum of products.

Excel ISNUMBER function
The Excel ISNUMBER function returns TRUE when a cell contains a number, and FALSE if not.

Excel FIND function
The Excel FIND function is used to find a string within another string, and returns the starting position of the string inside another one.


Related formulas

Count cells that begin or end with specific text
This article is going to show you how to count cells that begin or end with specific text in a range in Excel with the COUNTIF function.

Count Blank/Nonblank Cells
This article explains formulas to count the number of blank and nonblank cells in a range in Excel.

Count cells that contain specific text with case sensitive
This tutorial provides a formula in details to count the number of cells that contains specific text with case sensitive in a range in Excel.

Count how many cells contain errors
This tutorial shows how to count the number of cells that contains errors (any kind of errors, such as #N/A error, #VALUE! error or #DIV/0! error) in a specified range in Excel.


The Best Office Productivity Tools

Kutools for Excel - Helps You To Stand Out From Crowd

Would you like to complete your daily work quickly and perfectly? Kutools for Excel brings 300 powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) and save 80% time for you.

  • Designed for 1500 work scenarios, helps you solve 80% Excel problems.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands.
  • Become an Excel expert in 3 minutes. No longer need to remember any painful formulas and VBA codes.
  • 30-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years.
Ribbon of Excel (with Kutools for Excel installed)

Office Tab - Enable Tabbed Reading and Editing in Microsoft Office (include Excel)

  • One second to switch between dozens of open documents!
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Brings Efficient Tabs to Office (include Excel), Just Like Chrome, Firefox, And New Internet Explorer.
Screen Shot of Excel (with Office Tab installed)
Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations