在单个单元格中查找多个值(以逗号分隔),然后将值返回到单个单元格(也以逗号分隔)

时间:2016-08-14 13:32:40

标签: excel-vba excel-formula vlookup vba excel

我需要在sheet1的A列上使用单元格A1,A2值

进行Vlookup
SD-121, SD-232, SD-23
SD-323,SD-333

等等.. 带有列A,B,C,D的不同表中的vLookup表。列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

2 个答案:

答案 0 :(得分:0)

您可以创建用户函数以通过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

那很快又脏,但它给了我想要的输出。根据需要进行调整。

答案 1 :(得分:0)

如果您从2016年2月开始拥有Office 365最新更新,则可以使用以下数组公式:

=TEXTJOIN(",",TRUE,IF(ISNUMBER(SEARCH(Sheet2!$A$1:$A$5,A1)),Sheet2!$B$1:$B$5,""))

作为数组公式,必须在退出编辑模式时使用Ctrl-Shift-Enter而不是Enter确认。如果操作正确,Excel会将{}放在公式周围。

返回将按照表2中的列表顺序,而不是逗号分隔字符串的顺序。

enter image description here