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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
wCJanssen, glad your sorted and thanks to Bill for the solution.

If the names might contain non-alphabetical characters (e.g. "." "-" "/" ) then the test
Code:
If UCase(sChr) = sChr And sChr <> " " Then
would result in 'surnames' that had some of the forename(s) in them. Try this bit instead
Code:
If sChr >="A" And sChr<="Z" Then
 
Upvote 0

Forum statistics

Threads
1,221,443
Messages
6,159,907
Members
451,601
Latest member
terrynelson55

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