Livin404
Well-known Member
- Joined
- Jan 7, 2019
- Messages
- 743
- Office Version
- 365
- 2019
- Platform
- Windows
Greetings, I'm trying to switch the first and last names in Excel. I think my current VBA is in the right neighborhood, but I need help in better defining it. In the cell I could have Sheridan Whiteside Ulysses III with a VBA I would like it to read Whiteside Sheridan Ulysses III. Bottom line I would need the option to switch the first and second word. The Delimiter will ALWAYS be a space (" "). Thank you.
VBA Code:
Sub ReverseName()
Set myRange = Application.Selection
Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", myRange.Address, Type:=8)
myDelemiter = (" ")
For Each myCell In myRange
xValue = myCell.Value
NameList = VBA.Split(xValue, " ")
If UBound(NameList) = 1 Then
myCell.Value = NameList(1) + myDelemiter + NameList(0)
End If
Next
End Sub