In this article, you will learn how to extract the nth word from text string. We will use VBA code to create UDF to find the nth word in a string.
We need to follow the below steps:
Enter the following code in the Module
Function ExtractNthWord(x As String, y As Integer)
Dim word() As String
Dim wordCount As Long
word = VBA.Split(x, " ")
wordCount = UBound(word)
If wordCount< 1 Or (y - 1) >wordCount Or y < 0 Then
ExtractNthWord = ""
ExtractNthWord = word(y - 1)
Following is the snapshot of the data:
Now, we only need to change the number in cell D1 & the results will automatically be changed accordingly.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.