问题
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