Excel Exact Word Matching

前端 未结 5 850
遥遥无期
遥遥无期 2021-01-23 00:38

Let\'s say I have \"Vegas is great\" in cell A1. I want to write a formula that looks for the exact word, \"gas\" in cells. Vegas ≠ gas, but the only search formula I\'m finding

5条回答
  •  悲&欢浪女
    2021-01-23 01:17

    One can also use regular expressions in VBA to accomplish this. In Regular Expressions, "\b" represents a word boundary. A word boundary is defined as the position between a word and a non-word character or the beginning or end of the line. Word characters are [A-Za-z0-9_] (letters, digits, and the underscore). Hence, one can use this UDF. You do need to be aware that words which include non-word characters (e.g. a hyphen) may be treated differently than you expect. And if you are dealing with non-English letters, the Pattern would need to be modified.

    But the code is fairly compact.

    Option Explicit
    Function reFindWord(FindWord As String, SearchText As String, Optional MatchCase As Boolean = False) As Boolean
        Dim RE As Object
        Dim sPattern As String
    Set RE = CreateObject("vbscript.regexp")
    sPattern = "\b" & FindWord & "\b"
    With RE
        .Pattern = sPattern
        .ignorecase = Not MatchCase
        reFindWord = .test(SearchText)
    End With
    End Function
    

提交回复
热议问题