Count matching values, except...

BWMagee

Board Regular
Joined
Feb 18, 2014
Messages
112
The formula I wish to write has three rules:
1) count values in $E21:$AJ21
2) that match a value in $E$21:$A$J164
3) except when text in $C$21:$C$164 = $C21

Eg [using range E21:H24 instead of E21:AJ164].
CEFGH
21One1234
22Two1.52.53.54.5

23
Three1.8244.1
24One1
2
34

The correct answer here would be 2. From row 21, we can see the value 2 also appears in row 23 (so thats a count of 1), as does the value 4 (so thats a count of 1). They are the only values to appear more than once, so 1+1=2. We can see that every value in row 21 is found again in row 24, but as the text in C21 is the same as C24, we are ignoring that row.

I currently have the below formula, which calculates rules 1 and 2 perfectly, but I don't know how to include rule 3 within it. Any ideas?

=COUNT($E21:$AJ21)-SUMPRODUCT(--(COUNTIF($E$21:$AJ$164,$E21:$AJ21)=1))

All help is much appreciated!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Couple questions:

1) Will you be copying this formula down or over?
2) Why are you referencing starting in row 21, will you always?
 
Upvote 0
I will be copying this formula down. Not over.
I'm starting in row 21 as that is where the table starts. The table is from 21 to 164. There will be other tables using the same formula, and they will have a different starting point, but I don't mind if I have to change the starting point individually for each table.

Given the number of tables using this, it will feature in thousands of rows. I'm a little concerned SUMPRODUCT may slow the sheet down too much over that span, but won't know until I've tried it. If so, the formula may need to be changed to SUMIF instead (if it can).
 
Upvote 0
So in the above example when copying down to cell A22, do you still need to reference row 21 or only row 22 and below?
 
Upvote 0
Will the below give you what you are looking for? If it were me, I'd go with the helper columns as mentioned below. Let me know if this works for you.

A21 copied down:
Rich (BB code):
=SUM(H22:K22)

H21 copied down and over:
Rich (BB code):
=SUMPRODUCT((D22=$D$22:$G$25)*($C$22:$C$25<>$C22))

ABCDEFGHIJK
NumberAmount 1Amount 2Amount 3Amount 4Helper 1Helper 2Helper 3Helper 4
2​
One 1.00 2.00 3.00 4.00
0​
1​
0​
1​
0​
Two 1.50 2.50 3.50 4.50
0​
0​
0​
0​
4​
Three 1.80 2.00 4.00 4.10
0​
2​
2​
0​
2​
One 1.00 2.00 3.00 4.00
0​
1​
0​
1​
 
Upvote 0
Thanks very much Cyrus, that works! I hadn't thought of helper columns.
I can also rewrite the formula if I need to avoid the SUMPRODUCT formula:
=COUNTIF($D$2:$G$5,D2)-COUNTIFS($C$2:$C$5,$C2,D$2:D$5,D2)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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