在单个单元格中查找多个值(以逗号分隔),然后 return 将值添加到单个单元格(也以逗号分隔)
Lookup multiple values in a single cell (separated by commas) and then return the values to a single cell (also comma separated)
我需要在 sheet1 的 A 列上使用单元格 A1、A2 值进行 Vlookup
SD-121, SD-232, SD-23
SD-323,SD-333
等等..
vLookup table 在不同的 sheet 中包含 A、B、C、D 列。A 列具有
A B
SD-232 US
SD-23 UK
SD-323 IN
SD-333 SG
SD-121 CN
查找结果将显示在 sheet1 个结果单元格 B1 和 B2 的 B 列中
B
CN, US, UK
IN, SG
您可以创建一个用户函数来通过 VLOOKUP 函数循环值:
Function VLOOKUPARRAY(ByVal lookup_val As Range, ByVal table_array As Range, ByVal col_index_num As Integer, Optional ByVal range_lookup As Integer = 0) As String
Dim s As String
Dim a1() As String
Dim a2() As Variant
Dim i As Integer
'Recalculate whenever a calculation happens on the worksheet
Application.Volatile
'Get the lookup value from the cell
s = lookup_val.Value
'Split into array
a1 = Split(s, ",")
'Set output array to input array dimensions
ReDim a2(0 To UBound(a1))
'Loop through input array and set output array elements to lookup results using application lookup function.
For i = 0 To UBound(a1)
a2(i) = Application.WorksheetFunction.VLookup(Trim(a1(i)), table_array, col_index_num, range_lookup)
Next i
'Loop through output array and load values into a string
s = ""
For i = 0 To UBound(a2)
s = s & a2(i) & ", "
Next i
'Knock the final ", " off the end of the string
s = Left(s, Len(s) - Len(", "))
'Set function output to string value.
VLOOKUPARRAY = s
End Function
那是又快又脏,但它给了我想要的输出。根据需要进行调整。
如果您拥有 2016 年 2 月的 Office 365 最新更新,那么您可以使用以下数组公式:
=TEXTJOIN(",",TRUE,IF(ISNUMBER(SEARCH(Sheet2!$A:$A,A1)),Sheet2!$B:$B,""))
作为数组公式,退出编辑模式时必须使用 Ctrl-Shift-Enter 而非 Enter 进行确认。如果正确完成 Excel 将在公式周围放置 {}
。
return 将按照 sheet 2 上的列表顺序,而不是逗号分隔字符串的顺序。
我需要在 sheet1 的 A 列上使用单元格 A1、A2 值进行 Vlookup
SD-121, SD-232, SD-23
SD-323,SD-333
等等.. vLookup table 在不同的 sheet 中包含 A、B、C、D 列。A 列具有
A B
SD-232 US
SD-23 UK
SD-323 IN
SD-333 SG
SD-121 CN
查找结果将显示在 sheet1 个结果单元格 B1 和 B2 的 B 列中
B
CN, US, UK
IN, SG
您可以创建一个用户函数来通过 VLOOKUP 函数循环值:
Function VLOOKUPARRAY(ByVal lookup_val As Range, ByVal table_array As Range, ByVal col_index_num As Integer, Optional ByVal range_lookup As Integer = 0) As String
Dim s As String
Dim a1() As String
Dim a2() As Variant
Dim i As Integer
'Recalculate whenever a calculation happens on the worksheet
Application.Volatile
'Get the lookup value from the cell
s = lookup_val.Value
'Split into array
a1 = Split(s, ",")
'Set output array to input array dimensions
ReDim a2(0 To UBound(a1))
'Loop through input array and set output array elements to lookup results using application lookup function.
For i = 0 To UBound(a1)
a2(i) = Application.WorksheetFunction.VLookup(Trim(a1(i)), table_array, col_index_num, range_lookup)
Next i
'Loop through output array and load values into a string
s = ""
For i = 0 To UBound(a2)
s = s & a2(i) & ", "
Next i
'Knock the final ", " off the end of the string
s = Left(s, Len(s) - Len(", "))
'Set function output to string value.
VLOOKUPARRAY = s
End Function
那是又快又脏,但它给了我想要的输出。根据需要进行调整。
如果您拥有 2016 年 2 月的 Office 365 最新更新,那么您可以使用以下数组公式:
=TEXTJOIN(",",TRUE,IF(ISNUMBER(SEARCH(Sheet2!$A:$A,A1)),Sheet2!$B:$B,""))
作为数组公式,退出编辑模式时必须使用 Ctrl-Shift-Enter 而非 Enter 进行确认。如果正确完成 Excel 将在公式周围放置 {}
。
return 将按照 sheet 2 上的列表顺序,而不是逗号分隔字符串的顺序。