Switching position of last name & first name strings without affecting position of an existing middle name string

jnathan

New Member
Joined
Jul 8, 2013
Messages
48
My data shows employee names in the following convention; lastname middlename firstname e.g. Persie Van Robin. I need to format the order of the name so that it appears as firstname middlename lastname e.g. so it appears as Robin Van Persie. A middlename will in many cases not exist.

My macro successfully swaps the name convention order as required when a middlename does not exist however if it does then it treats the middlename and lastname as one single lastname e.g. the name will be formatted as Van Persie Robin.

Is anyone able to adapt the macro I have to solve the problem described above? Any help will be greatly appreciated!!! :) The macro is below:

Sub NameSwap()
' this macro swaps positions of lastname and firstname string

Dim i, LastRow

' switches last & first names around
LastRow = Range("B" & Rows.Count).End(xlUp).Row ' this line simply counts the lastrow to know when to stop
For i = 1 To LastRow
lname = Application.Proper(Mid(Cells(i, "B"), _
Application.Find(" ", Cells(i, "B"), 1)))
fname = Application.Proper(Mid(Cells(i, "B"), 1, _
Application.Find(" ", Cells(i, "B"), 1) - 1))
If Cells(i, "B").Value <> "" And _
Application.Find(",", Cells(i, "B"), 1) = 0 Then
Cells(i, "B").Value = lname & "" & fname
End If
Next

End Sub
 
And what if the middle name is the only part of the name that is potentially spelt with a lower case? i.e. as many Dutch names are - Robin van Persie

Ah, found it. It's in the Proper statements. McDonald is changed to Mcdonald.

If the original names are already properly cased (i.e. Persie instead of PERSIE), you can leave out the Application.Proper statements
 
Upvote 0
Then only use Proper (or if it needs to be lower case, use Lower) on the mName, not on lName and fName
 
Upvote 0
Although the below doesn't quite work - is 'Lower' being placed correctly?

mName = Application.Lower(Replace(.Value, lName, "")
 
Upvote 0
Try this:

Code:
Sub NameSwap()
' this macro swaps positions of lastname and firstname string
Dim i, LastRow
Dim lName As String, fName As String, mName As String
Dim tempName As String
' switches last & first names around
LastRow = Range("B" & Rows.Count).End(xlUp).Row ' this line simply counts the lastrow to know when to stop
For i = 1 To LastRow
    
    tempName = RTrim(Cells(i, "B").Value)
    
    If tempName <> "" Then
        lName = Left(tempName, InStr(tempName, " ") - 1)
        fName = Right(tempName, Len(tempName) - InStrRev(tempName, " "))
        mName = Replace(tempName, lName, "")
        mName = Trim(Replace(mName, fName, ""))
        If Len(mName) > 0 Then mName = LCase(mName) & " "
        Cells(i, "B").Value = fName & " " & mName & lName
    End If
Next
End Sub
 
Upvote 0
Fantastic - many thanks for all your help and advice. Really helped me get to a place where things are working for me

:)

Try this:

Code:
Sub NameSwap()
' this macro swaps positions of lastname and firstname string
Dim i, LastRow
Dim lName As String, fName As String, mName As String
Dim tempName As String
' switches last & first names around
LastRow = Range("B" & Rows.Count).End(xlUp).Row ' this line simply counts the lastrow to know when to stop
For i = 1 To LastRow
    
    tempName = RTrim(Cells(i, "B").Value)
    
    If tempName <> "" Then
        lName = Left(tempName, InStr(tempName, " ") - 1)
        fName = Right(tempName, Len(tempName) - InStrRev(tempName, " "))
        mName = Replace(tempName, lName, "")
        mName = Trim(Replace(mName, fName, ""))
        If Len(mName) > 0 Then mName = LCase(mName) & " "
        Cells(i, "B").Value = fName & " " & mName & lName
    End If
Next
End Sub
 
Upvote 0

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