SUMPRODUCT/COUNTIF looking at whether a value is in one of several cells in a row

biomathics23

New Member
Joined
Sep 10, 2018
Messages
6
Hello and thanks for reading,

I have a setup similar to the following (but much larger), featuring a single column containing names (each cell in this column will have a name), and several columns whose cells may contain letters (though many do not). Each name is unique but more than one letter may appear in a single cell as follows:


[TABLE="width: 500"]
<tbody>[TR]
[TD]Row[/TD]
[TD]Names[/TD]
[TD]Letters1[/TD]
[TD]Letters2[/TD]
[TD]Letters3[/TD]
[TD]Letters4[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Yanni[/TD]
[TD]A,B[/TD]
[TD]A[/TD]
[TD][/TD]
[TD]A, C[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Zane[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Xerxes[/TD]
[TD][/TD]
[TD]B[/TD]
[TD][/TD]
[TD]A, B[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Yanni[/TD]
[TD]C[/TD]
[TD][/TD]
[TD][/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Yanni[/TD]
[TD][/TD]
[TD][/TD]
[TD]A[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Xerxes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]B,C[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Zane[/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I am trying to find a formula that captures the following notion: Given Xerxes and A, how many rows have a "1" in the Numbers column and also have at least one "A" somewhere in Letters1-4 columns? So for Xerxes and A, only row 3 satisfies that, so the formula should return "1". For Yanni and A, rows 1 and 5 work, so the formula should return "2", etc.

I would like to use this formula to then populate a grid like the one below (results shown are based on the above example):

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]Xerxes[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Yanni[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Zane[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]

I tried to manipulate the sumproduct and countifs functions in a variety of ways, but I could never figure out how to get it to look at several cells (in actuality 10+) in a single row for a given letter while still summing over all the rows. The only solution I could think of would be to add a helper column of concatenated columns Letters1-4, and then sumproduct or countifs with that helper column and the name column, but I would vastly prefer avoiding such a workaround.

Any thoughts/advice/solutions?
 

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
Hi,

I don't see a "Numbers" Column where you say the 1 should be?
 
Upvote 0
My apologies, I edited the post but apparently missed some parts. That should have been "Names" column and "Xerxes", not "Numbers" and "1". Since it is too late to edit here is how it should have read:



Hello and thanks for reading,

I have a setup similar to the following (but much larger), featuring a single column containing names (each cell in this column will have a name), and several columns whose cells may contain letters (though many do not). Each name is unique but more than one letter may appear in a single cell as follows:


[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD]Row[/TD]
[TD]Names[/TD]
[TD]Letters1[/TD]
[TD]Letters2[/TD]
[TD]Letters3[/TD]
[TD]Letters4[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Yanni[/TD]
[TD]A,B[/TD]
[TD]A[/TD]
[TD][/TD]
[TD]A, C[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Zane[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Xerxes[/TD]
[TD][/TD]
[TD]B[/TD]
[TD][/TD]
[TD]A, B[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Yanni[/TD]
[TD]C[/TD]
[TD][/TD]
[TD][/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Yanni[/TD]
[TD][/TD]
[TD][/TD]
[TD]A[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Xerxes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]B,C[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Zane[/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


I am trying to find a formula that captures the following notion: Given Xerxes and A, how many rows have "Xerxes" in the Names column and also have at least one "A" somewhere in Letters1-4 columns? So for Xerxes and A, only row 3 satisfies that, so the formula should return "1". For Yanni and A, rows 1 and 5 work, so the formula should return "2", etc.

I would like to use this formula to then populate a grid like the one below (results shown are based on the above example):

[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]Xerxes[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Yanni[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Zane[/TD]
[TD]1[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]


I tried to manipulate the sumproduct and countifs functions in a variety of ways, but I could never figure out how to get it to look at several cells (in actuality 10+) in a single row for a given letter while still summing over all the rows. The only solution I could think of would be to add a helper column of concatenated columns Letters1-4, and then sumproduct or countifs with that helper column and the name column, but I would vastly prefer avoiding such a workaround.

Any thoughts/advice/solutions?
 
Upvote 0
So this formula does not use sumproduct or countifs, but it works: (This assumes the cell containing the word "Row" is A1)

=MMULT(--(TRANSPOSE($B$2:$B$8=$G13)),N(MMULT(N(ISNUMBER(SEARCH(J$12,$C$2:$F$8))),TRANSPOSE(COLUMN($C$2:$F$8)^0))>0))

Basically it acts like sumproduct on steriods.

Let me know if you have any questions or concerns.
 
Upvote 0
My apologies, I edited the post but apparently missed some parts. That should have been "Names" column and "Xerxes", not "Numbers" and "1".

No problem, just needed to know if That was another requirement for the formula(s).

You can either do a SUM/COUNTIFS or a SUMPRODUCT, I'm showing the SUMPRODUCT as the SUM/COUNTIFS is a much longer formula and in case you have additional Letters Columns in the future:

One important note, your sample results seems incorrect.


Book1
ABCDE
1NamesLetters1Letters2Letters3Letters4
2YanniA,BAA, C
3Zane
4XerxesBA, B
5YanniCC
6YanniA
7XerxesB,C
8ZaneABC
9
10
11ABC
12Xerxes131
13Yanni413
14Zane111
Sheet402
Cell Formulas
RangeFormula
B12=SUMPRODUCT(($A$2:$A$8=$A12)*(ISNUMBER(SEARCH(B$11,$B$2:$E$8))))


B12 formula copied down and across to D14
 
Upvote 0
ExcelElliott: Steroids indeed! That formula does just what I wanted, though I will have to go through the different functions to try to understand how it actually works. Thanks so much!

jtakw: I tried that approach but got the same results you did, which are not what I wanted: your formula gets 3 for Xerxes and B, because it counts the two B's in row 4 separately. I just wanted to count each row once, regardless of if it had 1 B or multiple B's. I do not think sumproduct can get there (do correct me if I am wrong), but thanks for the suggestion.
 
Upvote 0
A bit shorter...


Book1
ABCDEFGHIJK
1NamesLetters1Letters2Letters3Letters4ABC
2YanniA,BAA, CXerxes121
3ZaneYanni212
4XerxesBA, BZane111
5YanniCC
6YanniA
7XerxesB,C
8ZaneABC
Sheet1


In I2 control+shift+enter, not just enter, copy across, and down:

=SUMPRODUCT(($A$2:$A$8=$H2)+0,(MMULT(ISNUMBER(SEARCH(I$1,$B$2:$E$8))+0,TRANSPOSE(COLUMN($B$2:$E$8)^0))>0)+0)

Note. It's the TRANSPOSE call that necessitates applyin control+shift+enter in this formula with SUMPRODUCT.
 
Upvote 0
jtakw: I tried that approach but got the same results you did, which are not what I wanted: your formula gets 3 for Xerxes and B, because it counts the two B's in row 4 separately. I just wanted to count each row once, regardless of if it had 1 B or multiple B's. I do not think sumproduct can get there (do correct me if I am wrong), but thanks for the suggestion.

Sorry, I misunderstood your question...
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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