Skip to content
Free Excel Tutorials
  • Home
  • Excel For Beginners
  • Excel Intermediate
  • Advanced Excel For Experts

Data Analysis

  • How to Create One and Two Variable Data Tables in Excel
  • How to calculate current stock or inventory in Excel
  • How to Use Solver Tool in Excel
  • Error Bars in Excel
  • How to calculate correlation coefficient Correlation in Excel

References

  • How to get last row in text data in Excel
  • Count unique text values with criteria
  • How to use Excel MMULT Function
  • How to use Excel COLUMN Function
  • How to use Excel ROWS Function

Data Validations

  • Excel Data validation exists in list
  • How To Create Drop-down List in Excel
  • Excel Data validation whole percentage only
  • Excel Data validation require unique number
  • Excel Data validation only dates between

Count cells that contain either x or y in Excel

by

This tutorial shows how to Count cells that contain either x or y in Excel using the example below;

Formula

=SUMPRODUCT(--((ISNUMBER(FIND("abc",range)) +  ISNUMBER(FIND("def",range)))>0))

Explanation

To count cells that contain either one value or another, you an either use a helper column then tally up the count, or a more complex single cell formula.

Background

When you count cells with “OR” criteria, you need to be careful not to double count. For example, if you are counting cells that contain “abc” or “def”, you can’t just add together two COUNTIF functions, because you may double count cells that contain both “abc” and “def”.

Worked Example:   How to get Excel workbook path only

Single cell solution

For a single cell solution, you can use SUMPRODUCT with an ISNUMBER + FIND combo. The formula in cell F4 is:

=SUMPRODUCT(--((ISNUMBER(FIND("abc",B4:B10)) +  ISNUMBER(FIND("def",B4:B10)))>0))

This formula is based on the formula here that locates text inside of a cell:

ISNUMBER(FIND("abc",B4:B10)

When given a range of cells, this snippet will return an array of TRUE/FALSE values, one value for each cell the range. Since we are using this twice (once for “abc” and once for “def”), we’ll get two arrays.

Next, we add these arrays together (with +), which creates a new single array of numbers. Each number in this array is the result of adding the TRUE and FALSE values in the original two arrays together. In the example shown, the array looks like this:

Worked Example:   Highlight cells that begin with in Excel

{2;0;2;0;1;0;2}

We need to add these numbers up, but we don’t want to double count. So we need to make sure any value greater than zero is just counted once. To do that, we force all values to TRUE or FALSE with “>0”, then force to 1/0 with the double-negative (–).

Finally, SUMPRODUCT adds these numbers up.

Helper column solution

With a helper column to check each cell individually, the problem is less complex. We can use COUNTIF with two values (provided as an “array constant”). The formula in C4 is:

=--(SUM(COUNTIF(B4,{"*abc*","*def*"}))>0)



COUNTIF will return an array that contains two items: a count for “abc” and a count for “def”. To prevent double counting, we add the items up and then force the result to TRUE/FALSE with “>0”. Finally, we convert the TRUE/FALSE values to 1’s and 0’s with a double negative (–).

Worked Example:   Count cells equal to one of many things

The final result is either 1 or 0 for each cell. To get a total for all cells in the range, you’ll simply sum the helper column.

Post navigation

Previous Post:

Count cells that are not blank in Excel

Next Post:

Count cells that begin with in Excel

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Learn Basic Excel

Ribbon
Workbook
Worksheets
Format Cells
Find & Select
Sort & Filter
Templates
Print
Share
Protect
Keyboard Shortcuts

Categories

  • Charts
  • Data Analysis
  • Data Validation
  • Excel Functions
    • Cube Functions
    • Database Functions
    • Date and Time Functions
    • Engineering Functions
    • Financial Functions
    • Information Functions
    • Logical Functions
    • Lookup and Reference Functions
    • Math and Trig Functions
    • Statistical Functions
    • Text Functions
    • Web Functions
  • Excel VBA
  • Excel Video Tutorials
  • Formatting
  • Grouping
  • Others

Logical Functions

  • IFERROR function: Description, Usage, Syntax, Examples and Explanation
  • How to use Excel XOR Function
  • Excel If, Nested If, And/Or Criteria Examples
  • How to use Excel FALSE Function
  • IFS function: Description, Usage, Syntax, Examples and Explanation

Date Time

  • Count holidays between two dates in Excel
  • How to calculate next anniversary date or birthday in Excel
  • YEARFRAC function: Description, Usage, Syntax, Examples and Explanation
  • Add decimal minutes to time in Excel
  • How to calculate future date say 6 months ahead in Excel

Grouping

  • Group arbitrary text values in Excel
  • Group times into unequal buckets in Excel
  • Calculate conditional mode with criteria in Excel
  • Categorize text with keywords in Excel
  • Group times into 3 hour buckets in Excel

General

  • How to calculate profit margin percentage in Excel
  • Transpose: Switch ‘Rows to Columns’ or ‘Columns to Rows’ in Excel
  • Flash Fill in Excel
  • AutoFit Column Width, AutoFit Row Height in Excel
  • Common Errors in Excel
© 2023 xlsoffice . All Right Reserved. | Teal Smiles | Abbreviations And Their Meaning