Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel:执行部分匹配时,在单个单元格中返回多个匹配项_Excel_Excel Formula_Vlookup_Lookup_Vba - Fatal编程技术网

Excel:执行部分匹配时,在单个单元格中返回多个匹配项

Excel:执行部分匹配时,在单个单元格中返回多个匹配项,excel,excel-formula,vlookup,lookup,vba,Excel,Excel Formula,Vlookup,Lookup,Vba,我正在处理一个有两列的文件。第一列有简单的三个单词的句子。第二个是单字关键字 我希望能够搜索第一列,找到所有具有特定关键字的句子,并将它们列为关键字旁边的分隔值 假设管道(“|”)作为分隔符,我会得到如下结果: First Column Very blue sky. Red sky tonight. Blue sky forever. My red car. Red red red. 第二栏如下: Second Column Blue Red 所需的解决方案(有两列,蓝色和红色在第一列

我正在处理一个有两列的文件。第一列有简单的三个单词的句子。第二个是单字关键字

我希望能够搜索第一列,找到所有具有特定关键字的句子,并将它们列为关键字旁边的分隔值

假设管道(“|”)作为分隔符,我会得到如下结果:

First Column
Very blue sky.
Red sky tonight. 
Blue sky forever. 
My red car. 
Red red red.
第二栏如下:

Second Column
Blue
Red
所需的解决方案(有两列,蓝色和红色在第一列)


谢谢

这里有一种方法

  • 按ALT+F11键打开Visual Basic编辑器(VBE)
  • 使用插入>>模块插入新模块
  • 在代码窗格中粘贴下面的代码

    Public Function ConcatPartLookUp(rngInput As Range, rngSource As Range, Optional strDelimiter As String, Optional blCaseSensitive)
    Dim rng As Range
    
    If strDelimiter = "" Then strDelimiter = "|"
    If IsMissing(blCaseSensitive) Then
        blCaseSensitive = False
    Else
        blCaseSensitive = True
    End If
    
    For Each rng In rngSource
        If blCaseSensitive Then
            If InStr(1, rng.Value, rngInput.Value, vbBinaryCompare) > 0 Then ConcatPartLookUp = ConcatPartLookUp & strDelimiter & rng.Value
        Else
            If InStr(1, rng.Value, rngInput.Value, vbTextCompare) > 0 Then ConcatPartLookUp = ConcatPartLookUp & strDelimiter & rng.Value
        End If
    Next
    
    If Len(ConcatPartLookUp) > 0 Then ConcatPartLookUp = Mid(ConcatPartLookUp, 2, Len(ConcatPartLookUp))
    
    End Function
    
  • 然后,您可以在工作表中使用此函数,就像任何其他正常函数一样,例如

    =ConcatPartLookUp(B2,A2:A6)

    请注意,我还提供了另外两个可选参数,从长远来看,它们可能会很有用。如果要使其区分大小写并传递不同的分隔符,如“#”,则需要使用:


    =ConcatPartLookUp(B2,A2:A6,#”,TRUE)

    解释清楚,可以给出解决方案,但请告诉我们您迄今为止尝试了什么。这个网站期待发起者的努力。我已经尝试用VLOOKUP解决这个问题,但是,我被卡住了,因为这个功能无法处理多次返回。如果你认为答案是可以的,那就太好了。谢谢你的“Shrivalabha Redij”-这很好地解决了问题!
    Public Function ConcatPartLookUp(rngInput As Range, rngSource As Range, Optional strDelimiter As String, Optional blCaseSensitive)
    Dim rng As Range
    
    If strDelimiter = "" Then strDelimiter = "|"
    If IsMissing(blCaseSensitive) Then
        blCaseSensitive = False
    Else
        blCaseSensitive = True
    End If
    
    For Each rng In rngSource
        If blCaseSensitive Then
            If InStr(1, rng.Value, rngInput.Value, vbBinaryCompare) > 0 Then ConcatPartLookUp = ConcatPartLookUp & strDelimiter & rng.Value
        Else
            If InStr(1, rng.Value, rngInput.Value, vbTextCompare) > 0 Then ConcatPartLookUp = ConcatPartLookUp & strDelimiter & rng.Value
        End If
    Next
    
    If Len(ConcatPartLookUp) > 0 Then ConcatPartLookUp = Mid(ConcatPartLookUp, 2, Len(ConcatPartLookUp))
    
    End Function