Excel Exact Word Matching

前端 未结 5 848
遥遥无期
遥遥无期 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:04

    I think the only way to cover all possible punctuation surrounding the search word is to create a custom macro function. Use the enhanced split function to tokenize the sentence into an array of words then search the array for a match.

    Enhanced split function https://msdn.microsoft.com/en-us/library/aa155763

    How to create custom macro http://www.wikihow.com/Create-a-User-Defined-Function-in-Microsoft-Excel

    Code to create FindEngWord function

    Public Function FindEngWord(ByVal TextToSearch As String, ByVal WordToFind As String) As Boolean
    
    Dim WrdArray() As String
    Dim text_string As String
    Dim isFound As Boolean
    
    isFound = False
    
    text_string = TextToSearch
    
    WrdArray() = Split(text_string)
    
    isFound = False
    For i = 0 To UBound(WrdArray)
        If LCase(WrdArray(i)) = LCase(WordToFind) Then
            isFound = True
        End If
    Next i
    
    FindEngWord = isFound
    
    End Function
    
    
    Public Function Split(ByVal InputText As String, _
             Optional ByVal Delimiter As String) As Variant
    
    ' This function splits the sentence in InputText into
    ' words and returns a string array of the words. Each
    ' element of the array contains one word.
    
        ' This constant contains punctuation and characters
        ' that should be filtered from the input string.
        Const CHARS = ".!?,;:""'()[]{}"
        Dim strReplacedText As String
        Dim intIndex As Integer
    
        ' Replace tab characters with space characters.
        strReplacedText = Trim(Replace(InputText, _
             vbTab, " "))
    
        ' Filter all specified characters from the string.
        For intIndex = 1 To Len(CHARS)
            strReplacedText = Trim(Replace(strReplacedText, _
                Mid(CHARS, intIndex, 1), " "))
        Next intIndex
    
        ' Loop until all consecutive space characters are
        ' replaced by a single space character.
        Do While InStr(strReplacedText, "  ")
            strReplacedText = Replace(strReplacedText, _
                "  ", " ")
        Loop
    
        ' Split the sentence into an array of words and return
        ' the array. If a delimiter is specified, use it.
        'MsgBox "String:" & strReplacedText
        If Len(Delimiter) = 0 Then
            Split = VBA.Split(strReplacedText)
        Else
            Split = VBA.Split(strReplacedText, Delimiter)
        End If
    End Function
    

    Can be called from your excel sheet with this.

    =FindEngWord(A1,"gas")
    

提交回复
热议问题