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:

[TABLE="width: 64"]
<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl65, width: 64"]code[/TD]
[/TR]
[TR]
[TD="class: xl65"]058[/TD]
[/TR]
[TR]
[TD="class: xl65"]03102[/TD]
[/TR]
[TR]
[TD="class: xl65"]0504[/TD]
[/TR]
</tbody>[/TABLE]

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?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Last edited:
Upvote 0
Since you know the number of rows in the range you could always use

=3-COUNTIF(A2:A4,"=058")
 
Upvote 0
Or this formula should work:

=COUNTA(A2:A4)-COUNTIF(A2:A4,"=058")
 
Upvote 0
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"?
 
Upvote 0
=SUMPRODUCT(--(A2:A4<>"058")) also works but it's hard to understand for me...
 
Upvote 0
Unfortunately yes. Any cell with 058 as part of it will be matched (e.g. 2058, 0058,0583, etc. Try my suggestion above.
 
Upvote 0
=SUMPRODUCT(--(A2:A4<>"058")) also works but it's hard to understand for me...

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
 
Upvote 0
Unfortunately yes. Any cell with 058 as part of it will be matched (e.g. 2058, 0058,0583, etc. Try my suggestion above.

and this is good, as it must count all the values that are not (exactly) "058".

So again: can you imagine a case where "<>*058*" is not equivalent to (a correctly-working) "<>058"?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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