根据单词列表搜索字符串列表并返回值 (Excel)

Seaching a list of strings againist a list of words and returning a value (Excel)

在 excel 我有两张纸。在 Sheet1 A 列中,我有字符串。在 Sheet2 中,我有两列,A 列包含一个可能位于 Sheet1 字符串中某处的单词,B 列有一个 return 值,如果 Sheet2!A:A 在 Sheet1!A:A 中的字符串中的任何位置被发现。例如:

Sheet 1:

Col. A                             **Col. B (These are the results I WANT from Sheet2)**
Whosebug excel blue pony        Group2
grapes monkey help me                Group1
random words not very creative       Group3

而在 Sheet 2 中,你有

 Col. A     Col. B
 monkey     Group1
 excel      Group2
 creative   Group3

我一直在搞乱基本上可以归结为 Sheet1!B:B 的 vlookup 的一堆不同迭代,但我永远无法让它正常工作。我一直关注 vlookups 的原因是因为需要 return Sheet2!B:B if Sheet2!A:A if Sheet 中的值=]1!A:A.我敢肯定这个解决方案可能有比这更好的东西,我只是坚持这一点。任何帮助将不胜感激。

一点 VBA 可能是你最好的选择...

Sub finder()

Dim ws1, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

Dim i As Integer

i = 1

Do Until ws2.Cells(i, 1).Value = ""
'The next line takes the value from sheet2, looks for it in sheet1 then puts 
'the value of the cell directly adjacent back into sheet2. This may fail if a 
'value is not found.

ws2.Cells(i, 2).Value = ws1.Range("A:A").Find(ws2.Cells(i, 1).Value).Offset(0, 1).Value
    i = i + 1
Loop

End Sub

由于修改了问题而修改了公式

尝试将 Sheet1 B2 中的公式复制下来

=LOOKUP(2^15,SEARCH(Sheet2!A:A,A2),Sheet2!B:B)

这应该适用于您的示例,但在实际情况中,您可能会得到错误的匹配,因为例如 "key" 的搜索值将与 "monkey" 匹配。为了防止你可以使用这个只匹配整个单词的版本:

=LOOKUP(2^15,SEARCH(" "&Sheet2!A:A&" "," "&A2&" "),Sheet2!B:B)