Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I have a UDF to write out the names in proper naming style.
But, I am not able to incorporate names such as, say for example, Douglas MacArthur. Instead of Douglas MacArthur, the UDF outputs Douglas Macarthur.
How can I tweak the UDF to handle names of the types as Douglas MacArthur?
The UDF is:
But, I am not able to incorporate names such as, say for example, Douglas MacArthur. Instead of Douglas MacArthur, the UDF outputs Douglas Macarthur.
How can I tweak the UDF to handle names of the types as Douglas MacArthur?
The UDF is:
Code:
Public Function NameStyle( _
ByVal strName As String) As String
Dim nameObjt As Object
Dim nameItem
Dim namePart
Dim nameText As String
Set nameObjt = CreateObject("vbscript.regexp")
nameObjt.ignorecase = True
nameObjt.Global = True
nameObjt.Pattern = "(^|\s|\-)+(.[^\s^-]+)"
Set nameItem = nameObjt.Execute(strName)
nameText = ""
For Each namePart In nameItem
nameObjt.ignorecase = True
nameObjt.Pattern = "(^|\s)+(van|von|der|de|la|di|al)($)"
If nameObjt.Test(namePart.SubMatches(1)) Then
nameText = nameText & namePart.SubMatches(0) & LCase$(namePart.SubMatches(1))
Else
nameText = nameText & namePart.SubMatches(0) & StyleName(namePart.SubMatches(1), nameObjt)
End If
Next
NameStyle = Trim(nameText)
End Function
Private Function StyleName(nameStrg As String, nameObjt As Object) As String
Dim nameText As String
Dim nameLeft As String
Dim slrMatch
Dim plrMatch
nameObjt.ignorecase = True
nameObjt.Global = False
nameText = UCase$(Left(nameStrg, 1)) & LCase$(Mid$(nameStrg, 2, Len(nameStrg) - 1))
nameObjt.Pattern = "(^|\s)+(Mc|[DO]\'|St\.|St[\.]? )([a-z]+)"
Set plrMatch = nameObjt.Execute(nameText)
For Each slrMatch In plrMatch
nameLeft = slrMatch.SubMatches(2)
nameLeft = UCase$(Left(nameLeft, 1)) & Mid$(nameLeft, 2, Len(nameLeft) - 1)
nameText = slrMatch.SubMatches(0) & slrMatch.SubMatches(1) & nameLeft
Next
nameObjt.Pattern = "(^|\s*)(Mac)([dr])([a-z -]+)"
Set plrMatch = nameObjt.Execute(nameText)
For Each slrMatch In plrMatch
nameText = slrMatch.SubMatches(1) & UCase(slrMatch.SubMatches(2)) & slrMatch.SubMatches(3)
Next
StyleName = nameText
End Function