countif doesn't return the correct result when using "<>"

6diegodiego9

Board Regular
Joined
Jan 9, 2018
Messages
80
Office Version
  1. 2016
Platform
  1. Windows
Take this table:

code
058
03102
0504

<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>
</tbody>

where the "numbers" are stored as text to preserve the leading zero(es).

The formula =COUNTIF(A2:A4;"=058") returns 1.

Why does the formula =COUNTIF(A2:A4;"<>058") returns 3 instead than 2?
 
Thanks for the answers!
As I don't know the number of rows in real case I found that the minimalist working "solution" is this:

=COUNTIF(A2:A4,"<>*058*") (it returns 2)


In real case with many more values in the source table, can you imagine a case where "<>*058*" is not equivalent to (a correctly-working) "<>058"?

An additional entry like 0588 would thwart that solution.


=SUM(COUNTIF(A2:A4,{"<>","058"})*{1,-1})

is an option for circumventing this erroneous behavior.
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You misunderstand me. Try replacing 0504 in your sample data with 0583 and your count will go down to 1.
 
Upvote 0
A2:A4<>"058" returns an array of True/False like this:

{False, True, True}

Unfortunately, True equates to -1 so the --() around it changes the "sign" of the values to get this:

{0, 1, 1}

You're then just summing up these values which returns 2.

You could also do it like this:

=SUM(--(A2:A4<>"058"))

But you'd have to use Ctrl+Shift+Enter to create an array formula for this.

WBD

Thanks for the explanation.
The "problem" is that SUMPRODUCT in the help page is designed to do a totally different thing that returning "an array of True/False":
It's designed to do this: "Multiplies corresponding components in the given arrays, and returns the sum of those products."

Therefore it would be difficult for other to understand it...
 
Upvote 0
I tried and I still get 2

Unfortunately I don't. Maybe its to do with the cell formatting. You do, however have at least 3 workable answers suggested (including mine), so I should go with one of them.
 
Upvote 0
0588 is different than 058 so it must be counted, like 1234 or 9999.
Why do you write that it would thwart my solution?

My set up does just that...

Row\Col
A​
B​
C​
1​
code
3​
2​
058
4​
3​
058
4​
2
5​
03102
6​
0588
7​
0504
8​

C1 >> (yours)

=COUNTIF(A2:A7,"<>*058*")

C2 >> the set up I suggested...

=SUM(COUNTIF(A2:A7,{"<>","058"})*{1,-1})
 
Upvote 0
0588 is different than 058 so it must be counted, like 1234 or 9999.

This is in direct agreement with aladin's (and my) formula and your suggestion does NOT produce the result we think you are after. See aladin's post above for proof. By the way, my suggestion was

=COUNTA(A2:A4)-COUNTIF(A2:A4,"=058") .......
 
Last edited:
Upvote 0
I apologize with everyone when I assumed that my formula counted 0588 as different, it didn't! sorry sorry sorry.

I also found that the solution with SUMPRODUCT don't work correctly when the numbers are stored as numbers.

=SUM(COUNTIF(A2:A4,{"<>","058"})*{1,-1}) instead works with numbers stored as text and as numbers as well.

same for =COUNTA(A2:A4)-COUNTIF(A2:A4,"=058")

Great solutions!
Thanks guys!!
 
Upvote 0

Forum statistics

Threads
1,215,515
Messages
6,125,274
Members
449,220
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top