匹配细胞内的任何单词与单元格范围内的任何单词 [英] Match Any Word Inside Cell With Any Word In Range of Cells

查看:145
本文介绍了匹配细胞内的任何单词与单元格范围内的任何单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个短语列表。我想检查任何新的术语是否与单词相符。



我正在寻找一个代码来实现模糊匹配,以返回具有接近匹配的单元格。



示例数据



 短语,术语
实际期限,新词
伟大的工作,新词汇
检查短语,更多短语
示例这里,伟大的阿尔法
短语随机,beta新

期望的输出: strong>



 短语,期限,匹配
实际期限,新词,无匹配
伟大的工作,新的术语,真正的术语
检查短语,更多短语,检查短语/短语随机
示例这里,伟大的阿尔法,伟大的工作
短语随机,beta新,没有匹配

我有什么: / p>

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

  = IF(ISERROR(MATCH(*& B2& *,A:A,0)),无匹配,VLOOKUP(*& B2&*,A:A,1,FALSE))

但是,代码只匹配整个单元格。我如何使它与单元格中的任何单词相匹配?这将创建一个模糊的匹配。任何积极的投入都非常感激。

解决方案

这是一个(粗糙而准备的)VBA解决方案。您将需要将其插入到VBA编辑器中的代码模块中,然后您可以运行该宏以获得所需的输出。

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

'确保您的工作簿具有正确的工作表名称
设置短语= ThisWorkbook.Worksheets(Sheet2)。范围(A2:A6)
设置术语= ThisWorkbook.Worksheets Sheet1)范围(D2:D6)

对于每个术语在
matches =
words()= Split(term.Value)

对于i = 0到UBound(words,1)
对于每个短语在短语
如果InStr(1,phrase.Value,words(i))然后
匹配=比赛&短语& /
End If
下一个短语
Next i

如果匹配<> vbNullString Then
term.Offset(0,5).Value = Left(matches,Len(matches) - 1)
Else
term.Offset(0,5).Value =No匹配
结束如果
下一项
结束子


I have a list of phrases. I would like to check if any new terms match that list partially by word.

I'm looking for a code to implement fuzzy matching on the list to return the cell that has a close match.

Example Data:

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

Desired Output:

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

What I've got:

I tried using the following code to match the cell if it is found:

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

However, the code only matches the entire cell. How can I make it match any word in the cell? This would create a fuzzy match. Any positive input is highly appreciated.

解决方案

Here is a (rough and ready) VBA solution to your question. You will need to insert it into a code module in the VBA editor and then you can run the macro to get your desired output

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 = ""
        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

这篇关于匹配细胞内的任何单词与单元格范围内的任何单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆