June 11, 2019

Find Specific Word in Text

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]
End Function

Here’s the code for the INSTR function:

Sub positionofString()
Dim lposition As Long
lposition = InStr(1, Range(“A1”), “game”)
Range(“F1”).Value = lposition
End Sub

One thought on “Find Specific Word in Text

  1. Hi Dr. Dinesh,

    I have been finding your videos useful for the past year.

    However i have been in search of a video where you developed a useform vba code that is able to search a range of data in a specific column and using partial information for example i’m trying to retrieve the details of this number 345678902678902349024, but would like the userform to search using the last 6 digits of this number then when i click “update” the information is edited in the database.

Comments are closed.