搜索字符串并从另一列返回多个值

时间:2015-09-21 19:26:24

标签: excel indexing lookup

Return Lookup Array Lookup Value    Expected Output
A   2   1   NA
B   2   2   A, B, C
C   2   3   D,E
D   3   4   F, G
E   3   5   NA
F   4   6   NA
G   4   7   NA

这就是表格的样子。第四列是我期望的返回值。我在第2列的第3列中使用查找值,输出在第4列。

1 个答案:

答案 0 :(得分:0)

由于您需要在同一个单元格中返回多个值,因此您可能需要一些VBA:

Function doLookup(lookupValue As String) As String
    Dim searchRange As Range
    Dim searchCell As Range
    Dim searchValue As String

    'What range are we searching here?
    Set searchRange = Sheet1.Range("B1:B" & Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row())

    'Loop through each cell in the range
    For Each searchCell In searchRange

        searchValue = searchCell.Value

        'If we find a match...
        If searchValue = lookupValue Then

            'If this is not the first thing in the list, then add a comma
            If doLookup <> "" Then doLookup = doLookup & ","

            'Add the thing we found
            doLookup = doLookup & searchCell.Offset(0, -1).Value
        End If
    Next searchCell
End Function

将其粘贴在一个新模块中,然后在D1中使用公式=DoLookup(C1),它应该吐出你想要的东西。如果列表非常大,那么不要期望这个太过分了。

相关问题