How to lookup a value based on two columns (column values are not unique)

戏子无情 提交于 2020-06-23 08:31:55

问题


This is my data:

File1
Name School     Age    Weight
Jack St John    15
Jack St Mary    14
Jack St Michael 12
Mary St John    16
Mary St Mary    12
Mary St Michael 15

Raw data

Name School Weight
Jack St John    80
Jack St Mary    75
Jack St Michael 95
Mary St John    75
Mary St Mary    65
Mary St Michael 80

I want to fetch Weight values referring Raw data.

I tried with MATCH and INDEX, however I kept on getting #VALUE!.

Any ideas what to use to fetch these Weight values?


回答1:


The conventional solution is to use a helper column to make the values unique. So for example in your Raw data insert a column C with =A1&"|"&B1 copied down to suit, then in File 1, D2:

=VLOOKUP(A2&"|"&B2,'Raw data'!C:D,2,0)  

copied down to suit.




回答2:


Here's pnuts answer laid out explicitly.

Raw data

Name    School   Weight Helper  
Jack    St John     80  Jack|St John
Jack    St Mary     75  Jack|St Mary
Jack    St Michael  95  Jack|St Michael
Mary    St John     75  Mary|St John
Mary    St Mary     65  Mary|St Mary
Mary    St Michael  80  Mary|St Michael

The formula in the helper column is:

=A2&"|"&B2 just as pnuts suggested

File1

Name    School      Age Weight
Jack    St John     15  80
Jack    St Mary     14  75
Jack    St Michael  12  95
Mary    St John     16  75
Mary    St Mary     12  65
Mary    St Michael  15  80

The formula in the Weight column is:

=INDEX('[Raw data.xlsx]Sheet1'!$C$2:$C$7,MATCH(A2&"|"&B2,'[Raw
data.xlsx]Sheet1'!$D$2:$D$7,0))

It worked first time out so I don't know why you're getting #Value.

Give credit to pnuts for this answer.



来源:https://stackoverflow.com/questions/25853094/how-to-lookup-a-value-based-on-two-columns-column-values-are-not-unique

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