Proper NAME case change UDF Error - Not handling few Names

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. 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:
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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Unfortunately, there is no simple solution to your problem. Whereas you would want code to change this...

Macarthur

to this...

MacArthur

that same code would change the president of France's name from this...

Macron

to this...

MacRon

which, of course, would not be correct. The only thing I can think of is to maintain a "dictionary" of names that need to be corrected and have your software search all entries for those names correcting them as needed.
 
Upvote 0
Can we build and exception list in the UDF?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top