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
所需的解决方案(有两列,蓝色和红色在第一列)
谢谢 这里有一种方法
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