Reputation: 647
I am looking for a formula that will count the number of cells in a range (say A1:A5) whose values match any of the values of another range (say B1:B3).
Edit: I am also looking for a formula that doesn't break randomly. See bottom.*
If A1:A5 is:
1
2
3
4
5
and B1:B3 is:
3
4
8
the answer should be:
2
since A3 and A4 match something in B1:B3.
I expected the method shown here to work: https://exceljet.net/formula/count-total-matches-in-two-ranges
=SUMPRODUCT(COUNTIF(range1,range2))
...but it doesn't. Maybe that's because the range sizes are not the same?
=COUNTIF(range1,range2)
also doesn't work.
(My second range of cells is also on another sheet, but I can't imagine that would matter.)
Seems like there should be a simple formula for this kind of thing!
*Edit:
I tried the formula above a 2nd time, for the trivial example I gave, and this time it worked. The formula is unreliable, though. It can fail for some unknown reason as shown below. It sometimes fails to read the 2nd COUNTIF range, and from testing today it seems to fail if there is no outer SUMPRODUCT. Why would an outer formula element matter??
...it fails (and returns 0 instead of the expected 1).
With the exact same formula fragment (unless my eyes deceive me) wrapped in an outer SUMPRODUCT()...
...it works. Why the difference??
Upvotes: 3
Views: 6818
Reputation: 54767
As Solar Mike mentioned in the comments: Perhaps count matches.
=COUNT(MATCH($A$1:$A$10,$B$1:$B$10,0))
=SUMPRODUCT(--ISNUMBER(MATCH($A$1:$A$10,$B$1:$B$10,0)))
=SUMPRODUCT(--(COUNTIF($B$1:$B$10,$A$1:$A$10)>0))
The COUNTIF
Trap
Upvotes: 2
Reputation: 27233
Yes it does matches please refer the image below,
=SUMPRODUCT(COUNTIF($A$1:$A$5,$B$1:$B$3))
or
You may try this formula, as well,
=SUMPRODUCT(--ISNUMBER(MATCH($A$1:$A$5,$B$1:$B$3,0)))
Upvotes: 3