【问题标题】:Search a string and return multiple values from another column搜索字符串并从另一列返回多个值
【发布时间】:2015-12-18 14:47:55
【问题描述】:
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 列中。

【问题讨论】:

    标签: excel indexing lookup


    【解决方案1】:

    由于您需要在同一个单元格中返回多个值,因此您可能需要在此处使用一些 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),它应该会输出您要查找的内容。如果列表非常大,那么不要指望这太活泼了。

    【讨论】:

    • 我新建了一个模块,进入了查找栏。它没有返回任何东西。感谢您顺便调查一下。
    • 你是什么意思,“进入查找列”?
    • 我修改了脚本,改用 D 列。我做了doLookup(g2)。我确保工作表名称和查找数组列匹配。我得到“#VALUE!”
    • 所以您正在使用Set searchRange... 行查找 D?您正在查找的值在g2 中,但您得到的是#Value?这是很难通过互联网诊断的。唯一可能很时髦的是上面写着doLookup = doLookup &amp; searchCell.Offset(0, -1).Value 的那一点。这是假设您要以逗号分隔到列表中的值位于C 列中,因为那是来自D-1。我仍然无法想象为什么会通过#VALUE
    • 是的。所以这是我唯一编辑的东西。设置 searchRange = SHEETNAME.Range("D1:D" & SHEETNAME.Range("D" & SHEETNAME.Rows.Count).End(xlUp).Row())
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多