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
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!!!

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