[英]How to use vlookup if lookup value contains multiple values in single cell
Suppose Sheet1 has the following table ( data range Cell A1 to B4) 假设Sheet1具有下表(数据范围单元格A1到B4)
Fruits | Colors
======= | =======
Apple | Red
Mango | Green
Banana | Yellow
Suppose in sheet2, Cell A1 has following values ( Apple Mango Banana) separated based on space. 假设在sheet2中,Cell A1具有基于空格分隔的以下值(Apple Mango Banana)。
How to use VLookup in Cell B2 based on A1 value which can give you the results in the same order ( Red Green Yellow) 如何在单元格B2中使用VLookup基于A1值,可以按相同顺序给出结果(红绿黄)
This formula in Sheet2!B2
does it: Sheet2!B2
中的这个公式可以做到:
=INDEX(sheet1!B:B,MATCH(TRIM(LEFT(SUBSTITUTE(A1, " ", REPT(" ", 100)),25)),sheet1!A:A,0)) & " " &
INDEX(sheet1!B:B,MATCH(TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",100)),100,25)),sheet1!A:A,0)) & " " &
INDEX(sheet1!B:B,MATCH(TRIM(RIGHT(SUBSTITUTE(A1, " ", REPT(" ", 100)),25)),sheet1!A:A,0))
This formula works fine when you have 3 values to match (in case there are 2 only, the second one will be repeated). 如果要匹配3个值,则此公式可以正常工作(如果只有2个值,则第二个将重复)。 It can be further complicated to treat any number of values.
处理任意数量的值可能更复杂。
Of course it can be simpler in VBA using the Split
function, and it would take into account any number of keys, yielding the joined values. 当然,在VBA中使用
Split
函数可以更简单,并且它会考虑任意数量的键,从而产生连接值。 You can use this in a macro: 您可以在宏中使用它:
Sheet2.Range("B1").value = Join(Application.index(Sheet1.Columns("B"), _
Application.match(Split(Sheet2.Range("A1")), Sheet1.Columns("A"), 0)), " ")
Of course you can adjust it to any cell in column B
, using Range("B" & i)
and Range("A" & i)
... 当然你可以使用
Range("B" & i)
和Range("A" & i)
将它调整到B
列中的任何单元格...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.