devxlogo

InstrWord – Search a whole word

InstrWord – Search a whole word

' Return the next occurrence of a whole wordFunction InstrWord(start, Text, search, compareMethod) As Long    Dim index As Long    Dim charcode As Integer        ' assume the search fails    InstrWord = 0    index = start - 1        Do        ' search the next occurrence, exit if not found        index = InStr(index + 1, Text, search, compareMethod)        If index = 0 Then Exit Function                ' check that it is preceded by a punctuation symbol        If index > 1 Then            charcode = Asc(UCase$(Mid$(Text, index - 1, 1)))        Else            charcode = 32        End If        If charcode  90 Then            ' check that it is followed by a punctuation symbol            charcode = Asc(UCase$(Mid$(Text, index + Len(search), 1)) & " ")            If charcode  90 Then                InstrWord = index                Exit Function            End If        End If    LoopEnd Function

devx-admin

Share the Post: