where data is the named range B5:B14, and exclude is the named range D5:D7. This is an array formula and must be entered with control + shift + enter, except in Excel 365. Note: This formula is complex. See below for a more streamlined version of the formula based on the REDUCE function. Note: This formula is complicated by the “contains” requirement. If you just need a formula to count cells that do not equal many things, you can use a more straightforward formula based on the MATCH function.
COUNTIFS function
If you have a limited number of strings to exclude, you can use the COUNTIFS function like this: This formula uses the not equal to operator (<>) with the asterisk (*) wildcard to count cells that do not contain each string anywhere. However, with this approach, you must enter a new pair of range/criteria arguments for each string to exclude. In contrast, the formula explained below can handle a large number of strings to exclude entered directly in a range on the worksheet.
MMULT function
In the example shown, the formula in cell F5 is: This is a complex formula built around the MMULT function, which is designed to perform matrix multiplication. However, the core of the formula is based on the ISNUMBER and SEARCH functions: Here, we transpose the items in the named range “exclude”, then feed the result to SEARCH as the find_text, with data provided as within_text. The SEARCH function returns a 2d array of TRUE and FALSE values, 10 rows by 3 columns, like this: For each value in data, we have 3 results (one per search string) that are either #VALUE errors or numbers. Numbers represent the position of a found text string, and errors represent text strings not found. The TRANSPOSE function is needed to generate the 10 x 3 array of complete results. This array is fed into ISNUMBER to get TRUE FALSE values, which we convert to 1s and 0s with a double negative (–) operator. The result is an array like this:
MMULT function
The array above is provided to the MMULT function as array1. Following the rules of matrix multiplication, number of columns in array1 must equal the number of rows in array2. To generate array2, we use the ROW function like this: This yields an array of 1s, 3 rows by 1 column: which goes into MMULT as array2. After array multiplication, we have an array dimensioned to match the original data: In this array, any non-zero number represents a value where at least one of the excluded strings has been found. Zeros indicate no excluded strings were found. To force all non-zero values to 1, we use greater than zero: which creates yet another array of TRUE and FALSE values: Our final goal is to count only text values where no excluded strings were found, so we need to reverse these values. We do this by subtracting the array from 1. This is an example of boolean logic. The math operation automatically coerces TRUE and FALSE values to 1s and 0s, and we finally have an array to return to the SUM function: The SUM function returns a final result of 5.
REDUCE function
The REDUCE function, available in Excel 365, offers a more straightforward way to solve this problem. REDUCE loops over a range of data and applies a custom calculation to each cell, aggregating results to a single value using the calculation of your choice. To solve this problem with REDUCE, you can use a formula like this: The logic is similar to the formula described above, but simplified somewhat since we only need to check one cell at a time.
Dave Bruns
Hi - I’m Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.