문제

I have two tables and I want to count how many occurrences of an item by referencing another table,

for example, I want to know how many snakes Bob owns, the obvious answer is two but how do I do that in one formula? I have used sumproduct to calculate the number of occurrences of individual snakes but I want them all in one formula by referencing the second table. Is this possible?

Note: I have excel 2003, hence sumproduct

    A      B    
1  Bob    Boa
2  Ann    Cow
3  Bob    Sheep
4  Bob    Python

    A      B
1  Farm   Snake
2  Cow    Boa
3  Sheep  Python
도움이 되었습니까?

해결책

It's certainly possible to do this with your existing layout and without helper columns. Assume first table in A1:B4 and second table in D1:E3 and you can get the number of snakes Bob owns with this formula

=SUMPRODUCT((A1:A4="Bob")*ISNUMBER(MATCH(B1:B4,INDEX(D2:E3,0, MATCH("snake",D1:E1,0)),0)))

The first criterion is self-evident, I think.

For the second this part finds all the snake names:

INDEX(D2:E3,0,MATCH("snake",D1:E1,0))

....because it matches "snake" against the headers in D1:E1 and the returns the whole of the column below [by using zero as the row argument in INDEX you get the whole column].

Then, having all the snake names you can use MATCH to match B1:B4 against those. If there's a match you get a number, if not you get #N/A so you wrap the MATCH function in ISNUMBER to return TRUE/FALSE.

Another way is to use COUNTIF in place of ISNUMBER/MATCH

=SUMPRODUCT((A1:A4="Bob")*(COUNTIF(INDEX(D2:E3,0, MATCH("snake",D1:E1,0)),B1:B4)>0))

다른 팁

First of all, you need to set up your lookup table properly. There is no way to lookup with your second table as it is. First change it as follows:

Farm  Cow
Farm  Sheep
Snake Boa
Snake Python

Now you can lookup what type of item the thing is by using this formula:

=INDEX(Sheet2!A:A,MATCH(B1,Sheet2!B:B,0))

If you add that to column C in your original sheet, you will get this:

Bob Boa Snake
Ann Cow Farm
Bob Sheep   Farm
Bob Python  Snake

Then you can just use sumproduct:

=SUMPRODUCT((A1:A4="Bob")*(C1:C4="Snake"))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top