将单元格内的任何单词与单元格范围内的任何单词匹配

Match Any Word Inside Cell With Any Word In Range of Cells

我有一个短语列表。我想检查是否有任何新术语按单词部分匹配该列表。

我正在寻找一个代码来实现列表上的模糊匹配 return 具有接近匹配的单元格。

示例数据:

Phrases,Terms
real term,new words
great work,new term
check phrase,more phrase
example here,great alpha
phrase random,beta new

期望输出:

Phrases,Term,Match
real term,new words,No match
great work,new term,real term
check phrase,more phrase,check phrase/phrase random
example here,great alpha,great work
phrase random,beta new,No match

我得到的是:

我尝试使用以下代码来匹配找到的单元格:

=IF(ISERROR(MATCH("*" & B2 & "*",A:A, 0)), "No Match", VLOOKUP("*" & B2 & "*",A:A,1,FALSE))

但是,代码只匹配整个单元格。我怎样才能让它匹配单元格中的任何单词?这将创建一个模糊匹配。非常感谢任何积极的投入。

这是针对您的问题的(粗略的)VBA 解决方案。您需要将其插入 VBA 编辑器中的代码模块,然后您可以 运行 宏以获得所需的输出

Sub FindSimilar()
    Dim phrases As Range, phrase As Range
    Dim terms As Range, term As Range
    Dim matches As String
    Dim words() As String

    'ensure this has the correct sheet names for your workbook
    Set phrases = ThisWorkbook.Worksheets("Sheet2").Range("A2:A6")
    Set terms = ThisWorkbook.Worksheets("Sheet1").Range("D2:D6")

    For Each term In terms
        matches = vbNullString
        words() = Split(term.Value)

        For i = 0 To UBound(words, 1)
            For Each phrase In phrases
                If InStr(1, phrase.Value, words(i)) Then
                    matches = matches & phrase & "/"
                End If
            Next phrase
        Next i

        If matches <> vbNullString Then
            term.Offset(0, 5).Value = Left(matches, Len(matches) - 1)
        Else
            term.Offset(0, 5).Value = "No match"
        End If
    Next term
End Sub