简体   繁体   English

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

[英]Excel: Return multiple matches in a single cell while doing a partial match

I am working on a file that has two columns.我正在处理一个包含两列的文件。 The first column has simple three word sentences.第一列有简单的三个词的句子。 The second one has single word keywords.第二个是单字关键词。

I'd like to be able search the first column, find all sentences that have a specific keyword and list them as delimited values next to the keyword.我希望能够搜索第一列,找到所有具有特定关键字的句子,并将它们列为关键字旁边的分隔值。

Assuming a pipe (“|”) as a delimiter, I'd get something like this:假设一个管道(“|”)作为分隔符,我会得到这样的东西:

First Column
Very blue sky.
Red sky tonight. 
Blue sky forever. 
My red car. 
Red red red.

Second column is as follows:第二列如下:

Second Column
Blue
Red

Desired Solution (has 2 columns, Blue and Red are in the first column)所需的解决方案(有 2 列,蓝色和红色在第一列中)

Second Column         Results Column
Blue                  Very blue sky. | Blue sky forever. 
Red                   Red sky tonight. | My red car. | Red red red.

Thanks!谢谢!

Here is one way of doing it.这是一种方法。

  1. Open Visual Basic Editor (VBE) by pressing ALT+F11 key.按 ALT+F11 键打开 Visual Basic 编辑器 (VBE)。
  2. Insert a new module using Insert >> Module使用 Insert >> Module 插入一个新模块
  3. Paste below code in the code pane.在代码窗格中粘贴以下代码。

     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

Then you can use this function in sheet like any other normal function eg然后您可以像任何其他正常功能一样在工作表中使用此功能,例如

=ConcatPartLookUp(B2,A2:A6)

Please note I have provided two more optional arguments which may prove useful in the long run.请注意,我提供了另外两个可选参数,从长远来看可能会很有用。 If you want to make it case sensitive and pass a different delimiter say "#" then you need to use:如果你想让它区分大小写并传递一个不同的分隔符说“#”,那么你需要使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM