problem splitting names from prefixes

wCJanssen

New Member
Joined
Feb 22, 2009
Messages
24
Hi

I'm fairly new to vba, but managed to write a code that splits (Dutch) surnames from their prefixes. It works fine for fairly simple names, such as "van den Berg", but when you enter a double-barrelled name, such as "Baron Cohen" of "Rosario de Lima", it mistakes the first part of the name for a prefix. Here's what I've got so far:

Code:
Private Sub Test()
pos = InStrRev(txtSurnameIncl.Text, " ")
    txtSurnameExcl.Text = Mid$(txtSurnameIncl.Text, pos + 1, Len(txtSurnameIncl.Text) - pos)
    txtPrefixes.Text = Replace(txtSurnameIncl, " " & txtSurnameExcl.Text, "")
    If txtPrefixes = txtSurnameExcl Then txtPrefixes.Value = ""
txtPrefixes.Text = LCase(txtPrefixes.Text)
End Sub

Does anyone know how this can be solved? Maybe I can add a codeline that makes sure any word longer than three characters is regarded a name? Thanks in advance.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
wCJanssen, can you post some data please? Original plus what it should look like afterwards.
 
Upvote 0
sure, for instance:

"van den Berg" should be split into "van den" (prefix) and "Berg" (surname)
"de Boer" - "de" (prefix) and "Boer" (surname)
"van Oranje Nassau" - "van" (prefix) and "Oranje Nassau" (surname)
"Jansen" - "" (prefix) and "Jansen" (surname)
"Vennegoor of Hesselink" - "" (prefix) and "Vennegoor of Hesselink" (surname)

thanks for your reply.
 
Upvote 0
In can see a posible solution:

from your data it looks like the prefixes all have lower case initials. Prefix words could be removed upto the first Capital initial leaving the surname complete. Would that work?

One alternative would be a definitive lookup list of prefixes. But that could be a large list!
 
Upvote 0
Try

Code:
Option Explicit
Sub SplitName()
    Dim iLength As Integer
    Dim i As Integer
    Dim sChr As String
    Dim oName As Range
    For Each oName In Range("myRange")
        For i = 1 To Len(oName)
            sChr = oName.Characters(i, 1).Text
            If UCase(sChr) = sChr And sChr <> " " Then
                oName.Offset(0, 1) = Mid(oName, 1, i - 1)
                oName.Offset(0, 2) = Mid(oName, i, Len(oName))
                Exit For
            End If
        Next i
    Next oName
End Sub
 
Upvote 0
that works perfect; I forgot to mention that the name is in a userform textbox though, and should be split into two textboxes as well (sorry).
 
Upvote 0
I assume the textboxes are on a user form

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim i As Integer
    Dim sChr As String
        For i = 1 To Len(TextBox1)
            sChr = Mid(TextBox1.Value, i, 1)
            If UCase(sChr) = sChr And sChr <> " " Then
                TextBox2 = Mid(TextBox1.Value, 1, i - 1)
                TextBox3 = Mid(TextBox1.Value, i, Len(TextBox1.Value))
                Exit For
            End If
        Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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