Is there a way to swap around multiple names in one Excell cell?

Assie281

New Member
Joined
May 20, 2019
Messages
1
Hi there, is there a way (either formula or VB) to swap multiple names in one Excel cell?

Example of current data in one cell:
Victoria Wood, David Jones, Sharon Smith, Caroline S Petit, Martine P Smar, Elizabeth A Gilley, Alex Davies, Andrew P Collins

The format I want to convert it to (in one cell):
Wood V, Jones D, Smith S, Petit C, Smar M, Gilley E, Davis A, Collins P

Many thanks for looking into this,

Astrid
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi there. Assuming your data is as per your example (i.e. the surname is always immediately before the comma or on the end) and the desired initial is the first character of each name, this will do it:

Code:
Function SwapNames(thiscell As Range)
namelist = Split(thiscell.Value, ",")
For Each thisname In namelist
    lastSpace = InStrRev(thisname, " ")
    surname = Mid(thisname, lastSpace + 1)
    SwapNames = SwapNames +" "+ surname + " " + Left(LTrim(thisname), 1) + ","
Next
SwapNames = Left(SwapNames, Len(SwapNames) - 1)

End Function
Inpu it as a function in a module of your workbook, then use =SwapNames(A2) to get cell A2 reformatted. You will get a #value error if you enter a reference to more than 1 cell, or to an empty cell.
 
Last edited:
Upvote 0
Here is a slightly different way to write the SwapNames function...
Code:
Function SwapNames(S As String) As String
  Dim X As Long, Nm As Variant
  Nm = Split(S, ",")
  For X = 0 To UBound(Nm)
    Nm(X) = Trim(Nm(X))
    Nm(X) = Mid(Nm(X), InStrRev(Nm(X), " ") + 1) & " " & Left(Nm(X), 1)
  Next
  SwapNames = Join(Nm, ", ")
End Function
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,338
Members
448,570
Latest member
rik81h

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