How to find a specific word in a text string using the LIKE operator in a user defined function. The FindExactWordInString functions returns TRUE if a specific word is contained in a text string. You may think that this function is just another variation of Excel’s FIND or SEARCH functions or VBA’s Instr function. There’s an important difference. The FindExactWordInString function looks for a complete word. It can distinguish between ‘map’ and ‘mapped’, for example. The examples in the accompanying video clarify how our function works.
We have used the LIKE operator in our function which was originally shown by Excel MVP Rick Rothstein. The VBA Like operator allows us to compare strings against a pattern. We can also search a string to check if it contains another string, By searching if a string contains another string, Like does a similar job to the InStr string function. However, you’ll note that Like returns a TRUE or FALSE result whereas InSTR returns the starting position of the searched string in the text. Watch the video below:
Watch this video on YouTube.
Here’s the complete functions code:
Function FindExactWordInString(Text As String, Word As String) As Boolean
FindExactWordInString = ” ” & UCase(Text) & ” ” Like “[!A-Z]” & UCase(Word) & “[!A-Z]“
Here’s the code for the INSTR function:
Dim lposition As Long
lposition = InStr(1, Range(“A1”), “game”)
Range(“F1”).Value = lposition