Hey all, i am trying to split a users name but i am having problems with doing so. Below is my version if the user name has a “go by” name in brackets ():
theName = "Gates, Bill W. (Bill)"
hasBracket = False
If InStr(theName, "(") <> 0 Then
hasBracket = True
End If
If hasBracket = True Then
bracketPos = InStr(theName, ",")
lName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
theName = Trim(Mid(theName, bracketPos + 2))
bracketPos = InStr(theName, " ")
fName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
theName = Trim(Mid(theName, bracketPos))
If Trim(Left(theName, 1)) <> "(" Then
mName = StrConv(Replace(Trim(Left(theName, 1)), ".", ""), vbProperCase)
Else
mName = ""
End If
bracketPos = InStr(theName, "(")
bName = StrConv(Replace(Replace(Trim(Mid(theName, bracketPos)), "(", ""), ")", ""), vbProperCase)
Else
...
End If
That part works just fine as long as it has the “()” in the user name. Now going to the ELSE part where the “()” are not used is somewhat of a challenge for me it seems. Problem being is that the user name can be formatted as such:
Gates, Bill W.
Gates, Bill
Maybe i am just over thinking it but i can not seem to check if it has a middle name or not using the code from above for the ELSE section:
ELSE
bracketPos = InStr(theName, ",")
lName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
theName = Trim(Mid(theName, bracketPos + 2))
bracketPos = InStr(theName, " ")
If bracketPos <> 0 Then
fName = StrConv(Trim(Left(theName, bracketPos - 1)), vbProperCase)
End If
theName = Trim(Mid(theName, bracketPos))
If Trim(Left(theName, 1)) <> "(" Then
mName = StrConv(Replace(Trim(Left(theName, 1)), ".", ""), vbProperCase)
Else
mName = ""
End If
MsgBox lName & " " & fName & " " & mName
END IF
The code above works just fine if the user name is “Gates, Bill W.” but not if its “Gates, Bill”. What can i do in order to check to see if it has a middle name because currently, if it does not, then i get an error on line:
theName = Trim(Mid(theName, bracketPos))
Any help would be great, thanks! :o)
David
I dont know why i didnt think of this before! Solved! :o)