Complex countifs formula

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
I am providing a sample table below:

[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]A[/TD]
[/TR]
[TR]
[TD]A[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
</tbody>[/TABLE]

I can do a Countif to get the number of A's or B's. Or something like =SUM(COUNTIF($A$2:$A$7,{"A","B"})) to get the number of As and Bs.
I would like to refer to the part {"A","B"} as a cell reference, rather than have to manually type this.

Note, this is a condensed version for illustration only (so sumproduct would not be the best result for me).

Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi, like this maybe:


Excel 2013/2016
ABCDE
1AA5
2BB
3A
4A
5B
Saturday 2 (2)
Cell Formulas
RangeFormula
E1=SUMPRODUCT(COUNTIF(A1:A5,C1:C2))
 
Upvote 0
Thanks FormR,
I am trying to avoid this type of formula (if possible) as there are multiple items inside one cell (e.g. {"milk","juice","tea","coffee"}) and it would be preferable (if possible) to keep them listed like this in the one cell rather than splitting them into a single cell each.
 
Upvote 0
Hi, it is a little more complex but if you can set it up by not including the {} and "" characters as below then you can try:


Excel 2013/2016
ABCD
1AA,B8
2B
3A
4A
5B
6C
7A
8A
9B
10C
Sheet1
Cell Formulas
RangeFormula
D1=SUMPRODUCT(COUNTIF($A$1:$A$10,TRIM(MID(SUBSTITUTE(C1,",",REPT(" ",99)),ROW(INDEX(A:A,1):INDEX(A:A,LEN(C1)-LEN(SUBSTITUTE(C1,",",""))+1))*99-98,99))))
 
Last edited:
Upvote 0
Apologies, is it possible to take this one step further.

E.g.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD][/TD]
[TD]A,B[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]1[/TD]
[TD][/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

So the answer would be 6 based on finding A and B in the first column and where "1" is beside each A or B in the second column.

Thanks
 
Upvote 0
if you change your file in this way

[TABLE="class: grid"]
<tbody>[TR]
[TD][/TD]
[TD]
A
[/TD]
[TD]
B
[/TD]
[TD]
C
[/TD]
[TD]
D
[/TD]
[TD]
E
[/TD]
[/TR]
[TR]
[TD]
1
[/TD]
[TD]A[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD]a[/TD]
[TD]
6​
[/TD]
[/TR]
[TR]
[TD]
2
[/TD]
[TD]B[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD]b[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
3
[/TD]
[TD]C[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD]
1​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
4
[/TD]
[TD]A[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
5
[/TD]
[TD]A[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
6
[/TD]
[TD]A[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
7
[/TD]
[TD]B[/TD]
[TD]
1​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
8
[/TD]
[TD]B[/TD]
[TD]
2​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
9
[/TD]
[TD]B[/TD]
[TD]
2​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
10
[/TD]
[TD]C[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]



e1=
SUMPRODUCT(COUNTIFS(A1:A9,D1:D2,B1:B9,B1))
 
Upvote 0
is it possible to take this one step further.

Hi, not sure if you've already figured it out, but here is one way:


Excel 2013/2016
ABCDE
1A1A,B6
2B11
3C1
4A1
5A1
6A1
7B1
8B2
9B2
10C1
Sheet1
Cell Formulas
RangeFormula
E1=SUMPRODUCT(COUNTIFS($B$1:$B$10,D2,$A$1:$A$10,TRIM(MID(SUBSTITUTE(D1,",",REPT(" ",99)),ROW(INDEX(A:A,1):INDEX(A:A,LEN(D1)-LEN(SUBSTITUTE(D1,",",""))+1))*99-98,99))))
 
Upvote 0
Also, if you define List1 in the Name Manager as referring to:

={"milk","juice","tea","coffee"}

you can just have:

=SUMPRODUCT(COUNTIFS,List1))
 
Upvote 0
Thanks Aladin - I completely forgot about the named ranges - this works great.

Thanks also FormR and MARZIOTULLI, whose formulas also work.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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