Excel: Count unique text in column B when column A matches

别说谁变了你拦得住时间么 提交于 2021-01-04 07:17:10

问题


I'm trying to count unique text values in column B when column A matches.

Let's say I have

A 1    
A 1    
B 1    
B 2    
C 2    
C 2    
C 2

I want it to return

A has one unique text in column B    
B has two unique texts in column B    
C has one unique texts in column B

Which Excel formula would I use?


回答1:


With formula you are going to need a couple of helper columns:

In the first we put this array formula in the SECOND row:

=INDEX($A$1:INDEX(A:A,MATCH("zzz",A:A)),MATCH(0,COUNTIF($D$1:D1,$A$1:INDEX(A:A,MATCH("zzz",A:A))),0))

Then in the column next to it this array formula:

=SUM(IF($A$1:INDEX(A:A,MATCH("zzz",A:A))=D2,1/COUNTIFS($A$1:INDEX(A:A,MATCH("zzz",A:A)),D2,$B$1:INDEX(B:B,MATCH("zzz",A:A)),$B$1:INDEX(B:B,MATCH("zzz",A:A)))))

Both are array formulas. Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.

Then in the third column we can put the text:

=D2 & " has " & CHOOSE(E2,"one","two","three") & " unique " & IF(E2>1,"texts","text")& " in column B"

If you have greater than three unique you will need to put the text equivalents.




回答2:


In cell C1 you can enter the following array formula by pressing Ctrl + Shift + Enter:

=SUM(--(FREQUENCY(IF(A1:A7="A",B1:B7),B1:B7)>0))

Then in Cell D1 enter this normal formula:

"A has " & C1 & " unique text in column B"

Repeat in the next three rows down for B and C. If you really want to replace the numeral "1" with the word "one", that will be much harder and I do not recommend it. Also, if you really wanted to get fancy you can modify D1 to use an if statement to check whether or not C1 is more than 1 and if it is add the plural "s" onto the word "text".



来源:https://stackoverflow.com/questions/43265364/excel-count-unique-text-in-column-b-when-column-a-matches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!