Explanation
In this example, the goal is to count cells that do not contain a specific substring. This problem can be solved with the COUNTIF function or the SUMPRODUCT function . Both approaches are explained below. Although COUNTIF is not case-sensitive, the SUMPRODUCT version of the formula can be adapted to perform a case-sensitive count. For convenience, data is the named range B5:B15.
COUNTIF function
The COUNTIF function counts cells in a range that meet supplied criteria. For example, to count the number of cells in a range that contain “apple” you can use COUNTIF like this:
=COUNTIF(range,"apple") // equal to "apple"
Note this is an exact match. To be included in the count, a cell must contain “apple” and only “apple”. If a cell contains any other characters, it will not be counted. To reverse this operation and count cells that do not contain “apple”, you can add the not equal to (<>) operator like this:
=COUNTIF(range,"<>apple") // not equal to "apple"
The goal in this example is to count cells that do not contain specific text, where the text is a substring that can be anywhere in the cell. To do this, we need to use the asterisk (*) character as a wildcard . To count cells that contain the substring “apple”, we can use a formula like this:
=COUNTIF(range,"*apple*") // contains "apple"
The asterisk (*) wildcard matches zero or more characters of any kind, so this formula will count cells that contain “apple” anywhere in the cell. To count cells that do not contain the substring “apple”, we add the not equal to (<>) operator like this:
=COUNTIF(range,"<>*apple*") // does not contain "apple"
The formulas used in the worksheet shown follow the same pattern:
=COUNTIF(data,"<>*a*") // does not contain "a"
=COUNTIF(data,"<>*0*") // does not contain "0"
=COUNTIF(data,"<>*-r*") // does not contain "-r"
Data is the named range B5:B15. The COUNTIF function supports three different wildcards, see this page for more details.
Note the COUNTIF formula above won’t work if you are targeting a particular number and cells contain numeric data. This is because the wildcard automatically causes COUNTIF to look for text only (i.e. to look for “2” instead of just 2). In addition, COUNTIF is not case-sensitive, so you can’t perform a case-sensitive count. The SUMPRODUCT alternative explained below can handle both situations.
With a cell reference
You can easily adjust this formula to use a cell reference in criteria . For example, if A1 contains the substring you want to exclude from the count, you can use a formula like this:
=COUNTIF(range,"<>*"&A1&"*")
Inside COUNTIF, the two asterisks and the not equal to operator (<>) are concatenated to the value in A1, and the formula works as before.
Exclude blanks
To exclude blank cells, you can switch to COUNTIFS function and add another condition like this:
=COUNTIFS(range,"<>*a*",range,"?*") // requires some text
The second condition means “at least one character”.
SUMPRODUCT function
Another way to solve this problem is with the SUMPRODUCT function and Boolean algebra . This approach has the benefit of being case-sensitive if needed. In addition, you can use this technique to target a number inside of a number, something you can’t do with COUNTIF.
To count cells that contain specific text with SUMPRODUCT, you can use the SEARCH function . SEARCH returns the position of text in a text string as a number. For example, the formula below returns 6 since the “a” appears first as the sixth character in the string:
=SEARCH( "a","The cat sat") // returns 6
If the text is not found, SEARCH returns a #VALUE! error:
=SEARCH( "x","The cat sat") // returns #VALUE!
Notice we do not need to use any wildcards because SEARCH will automatically find substrings. If we get a number from SEARCH, we know the substring was found. If we get an error, we know the substring was not found. This means we can add the ISNUMBER function to evaluate the result from SEARCH like this:
=ISNUMBER(SEARCH( "a","The cat sat")) // returns TRUE
=ISNUMBER(SEARCH( "x","The cat sat")) // returns FALSE
To reverse the operation, we add the NOT function :
=NOT(ISNUMBER(SEARCH( "a","The cat sat"))) // FALSE
=NOT(ISNUMBER(SEARCH( "x","The cat sat"))) // TRUE
We now have what we need to count cells that do not contain a substring with SUMPRODUCT. Back in the example worksheet, to count cells that do not contain “a” with SUMPRODUCT, you can use a formula like this
=SUMPRODUCT(--NOT(ISNUMBER(SEARCH("a",data))))
Working from the inside out, SEARCH is configured to look for “a”:
SEARCH("a",data)
Because data (B5:B15) contains 11 cells, the result from SEARCH is an array with 11 results:
{1;1;1;1;2;2;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!}
In this array, numbers indicate the position of “a” in cells where “a” is found. The #VALUE! errors indicate cells where “a” was not found. To convert these results into a simple array of TRUE and FALSE values, we use the ISNUMBER function :
ISNUMBER(SEARCH("a",data))
ISNUMBER returns TRUE for any number and FALSE for errors. SEARCH delivers the array of results to ISNUMBER, and ISNUMBER converts the results to an array that contains only TRUE and FALSE values:
{TRUE;TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE}
In this array, TRUE corresponds to cells that contain “a” and FALSE corresponds to cells that do not contain “a”. This is exactly the opposite of what we need, so we use the NOT function to reverse the array:
NOT(ISNUMBER(SEARCH("a",data)))
The result from the NOT function is:
{FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;TRUE;TRUE;TRUE;TRUE}
In this array, the TRUE values represent cells we want to count. However, we first need to convert the TRUE and FALSE values to their numeric equivalents, 1 and 0. To do this, we use a double negative (–):
--NOT(ISNUMBER(SEARCH("a",data)))
The result inside of SUMPRODUCT looks like this:
=SUMPRODUCT({0;0;0;0;0;0;1;1;1;1;1}) // returns 5
With a single array to process, SUMPRODUCT sums the array and returns 5 as a final result.
One benefit of this formula is it will find a number inside a numeric value. In addition, there is no need to use wildcards to indicate position, because SEARCH will automatically look through all text in a cell.
Case-sensitive option
For a case-sensitive count, you can replace the SEARCH function with the FIND function like this:
=SUMPRODUCT(--NOT(ISNUMBER(FIND("A",data))))
The FIND function works just like the SEARCH function, but is case-sensitive. Notice we have replaced “a” with “A” because FIND is case-sensitive. If we used “a”, the result would be 11 since there are no cells in B5:B15 that contain a lowercase “a”. This example provides more detail .
Note: the SUMPRODUCT formulas above are more complex, but using Boolean operations in array formulas is a more powerful and flexible approach. It is also an important skill in modern functions like FILTER and XLOOKUP , which often use this technique to select the right data. The syntax used by COUNTIF is unique to a group of eight functions and is therefore not as useful or portable.
Explanation
In this example, the goal is to count the number of cells in a range that do not contain errors. The best way to solve this problem is to use the SUMPRODUCT function together with the ISERROR function. You can also use the COUNTIF function or COUNTIFS function to exclude specific errors. Both approaches are explained below.
COUNTIF function
One way to count cells that do not contain errors is to use the COUNTIF function like this:
=COUNTIF(B5:B14,"<>#N/A") // returns 9
For criteria, we use the not equal to operator (<>) with #N/A. Notice both values are enclosed in double quotes. COUNTIF returns 9 since there are nine cells in B5:B15 that do not contain the #N/A error. If we switch to the COUNTIFS function , we can exclude more than one kind of error like this:
=COUNTIFS(B5:B14,"<>#N/A",B5:B14,"<>#VALUE!") // returns 8
COUNTIFS returns 9 since there are eight cells in B5:B15 that do not contain the #N/A error or the #VALUE! error. However, this approach is tedious if the goal is to exclude all types of errors from the count. In that case, the SUMPRODUCT option below is more straightforward.
SUMPRODUCT function
The SUMPRODUCT function works directly with arrays and ranges. One thing you can do quite easily with SUMPRODUCT is perform a logical test on a range, then count the results. In this case, we want to count cells that do not contain errors and the simplest way to do that is with the ISERROR function and the NOT function. In the worksheet shown above, the formula in cell E5 is:
=SUMPRODUCT(--NOT(ISERROR(B5:B14)))
Working from the inside out, we first use the ISERROR function on the range B5:B14.
ISERROR(B5:B14) // check all 10 cells
Since there are ten cells in the range B5:B14, ISERROR returns an array with ten results like this:
{FALSE;TRUE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE}
Here, each TRUE value indicates a cell value that is an error. Since the goal is to count cells that do not contain errors, we reverse these results with the NOT function :
NOT({FALSE;TRUE;FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;TRUE})
which returns a new array like this:
{TRUE;FALSE;TRUE;TRUE;TRUE;FALSE;TRUE;TRUE;TRUE;FALSE}
Notice that each TRUE value now corresponds to a cell that does not contain an error. This array is now in the correct form. However, SUMPRODUCT only works with numeric data so the next step is to convert the TRUE and FALSE values to their numeric equivalents, 1 and 0. We do this with a double negative (–):
--{TRUE;FALSE;TRUE;TRUE;TRUE;FALSE;TRUE;TRUE;TRUE;FALSE}
The resulting array looks like this:
{1;0;1;1;1;0;1;1;1;0}
Finally, SUMPRODUCT sums the items in this array and returns the total, which in the example is the number 3:
=SUMPRODUCT({1;0;1;1;1;0;1;1;1;0}) // returns 7
ISERR function
Like the ISERROR function, the ISERR function returns TRUE when a value is an error. The difference is that ISERR ignores #N/A errors. If you want to count cells that do not contain errors, and ignore #N/A errors, you can substitute ISERR for ISERROR:
=SUMPRODUCT(--NOT(ISERR(B5:B14))) // returns 8
SUMPRODUCT returns 8 since there are eight cells that do not contain errors, ignoring the #N/A error in cell B14.