Here is a function I wrote to do the same but will be easier, as it is short,
more manageable, and easy to read when used as a formula in a cell.
It also has a tremendous flexibility to be used elsewhere.
It will return the nth word you specify and if there are non-alpha characters attached they will be removed.
FindWord("string value", Word Position# sought)
In a cell,
=FindWord(A1, -2) will return the 2nd to last word in the cell A1. (note the negative number passed)
You can retrieve the nth word starting from the Left or Right side.
If 'Position number' is
POSITIVE it fetches nth word from LEFT to RIGHT.
If 'Position number' is
NEGATIVE it fetches nth word from RIGHT to LEFT.
It allows a Hyphen in a word string. ie "Van-Stewart" will return "Van-Stewart".
If word begins or ends in a Hyphen, the Hyphen will be removed.ie "Over-" will return "Over".
If a hyphen is separated by spaces from other words, it is seen as a 'word'. ie " - " will return "-".
Otherwise only numbers and alpha characters are returned. ie "this," will return "this".
Languages that uses special characters or is specified in the system locale settings may break this(I can't test for this).
These accented characters À,Ê,à,ê,Ø,ø will work fine.
Example formula:
=FindWord("Her last name is Van-Stewart, she has goats, cats- and -fish.", 1) returns "Her"
=FindWord("Her last name is Van-Stewart, she has goats, cats- and -fish.", 3) returns "name"
=FindWord("Her last name is Van-Stewart, she has goats, cats- and -fish.", -1) returns "fish"
Note: Period removed
=FindWord("Her last name is Van-Stewart, she has goats, cats- and -fish.", 8) returns "goats"
Note: trailing comma removed
=FindWord("Her last name is Van-Stewart, she has goats, cats- and -fish.", -3) returns "cats"
Note: trailing hyphen removed
=FindWord("Her last name is Van-Stewart, she has goats, cats- and -fish.", 5) returns "Van-Stewart"
Note: hyphen isn't removed
Dealing with spaces, or no spaces, around a hyphen:
=FindWord("I have a house - not a- mouse.", 5) returns "-"
Note: hyphen surrounded by spaces is seen as a 'word'.
=FindWord("I have a house -not a- mouse.", 5) returns "not"
Both the STRING and the POSITION can be Cell references.
=FindWord(A1, B1)
To place this function in a Module
From your spreadsheet hit
[Alt]+
[F11] to open the Visual Basic Editor.
On the menu bar select
Insert > Module
Paste this code in the code window.
use in cells: =
FindWord(string to search, Word Position# sought)
Code:
Function FindWord(Source As String, Position As Integer) As String
Dim arr() As String
Dim sTmp As String, sChar As String, sClean As String
Dim xpos As Integer
' If Position number is POSIITIVE it fetches nth word from LEFT to RIGHT.
' If Position number is NEGATIVE it fetches nth word from RIGHT to LEFT.
'
' Note: We are allowing a Hyphen in a word string. ie "[COLOR=#333333]Van-Stewart[/COLOR]" will return "[COLOR=#333333]Van-Stewart[/COLOR]".
' If word begins or ends in a Hyphen, the Hyphen will be removed.ie "Over-" will return "Over".
' Otherwise only numbers and alpha characters are returned. ie "this," will return "this".
' Languages that uses special characters or is specified in the system locale settings may break this.
' These accented characters À,Ê,à,ê,Ø,ø are OK.
xpos = 0
sTmp = Trim(Source)
Do Until InStr(sTmp, " ") = 0 ' Loop until there are no more double spaces
sTmp = Replace(sTmp, " ", " ") ' Replace 2 spaces with 1 space
Loop
arr = Split(sTmp, " ")
xCount = UBound(arr)
If Position > 0 Then xpos = Position
If Position < 0 Then xpos = xCount + Position + 2
sTmp = Empty
If Not (((xpos - 1) > xCount) Or (xpos = 0)) Then sTmp = arr(xpos - 1)
' Check for characters to remove.
For i = 1 To Len(sTmp)
sChar = Mid$(sTmp, i, 1)
If sChar Like "[-0-9A-Za-zÀ-ø]" Then sClean = sClean & sChar
Next i
If sClean <> "-" Then ' If the string is just a hyphen(-) return it as the word.
If Left(sClean, 1) = "-" Then sClean = Mid(sClean, 2, Len(sClean))
If Right(sClean, 1) = "-" Then sClean = Mid(sClean, 1, Len(sClean) - 1)
End If
FindWord = sClean
End Function
As with any spreadsheet with code modules, be sure to allow macros to be run when spreadsheet is opened.