Switch first and last names

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. 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
 
@Livin404
You have typed comments inside one or multiple quotes, making it difficult to be sure what your comments relate to. Can you post again, making it clear & making your comments/questions outside (below) the quote(s)?
Yes I noticed that. The macro you provided is perfect, yet I have the macro listed below which prevents me from using your macro. I don’t understand as there are about 4 macro between. The VBA now errors out on myCell.Value = Trim(Join . If I remove my macro then your VBA works as it should.
VBA Code:
Sub Trim()
Dim cell As Range
For Each cell In Selection
  cell = Application.Trim(cell)
Next cell
End Sub
 
Last edited:
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I have the macro listed below which prevents me from using your macro. I don’t understand
Trim is a function in vba (& also worksheet function). It is a bad idea to name any procedure with the name of any existing function or 'special' word as that can cause problems as evidenced here.
Rename your 'Trim' routine to something else - say Trim_Cells
 
Upvote 0
Trim is a function in vba (& also worksheet function). It is a bad idea to name any procedure with the name of any existing function or 'special' word as that can cause problems as evidenced here.
Rename your 'Trim' routine to something else - say Trim_Cells
that did the job. You're quite good at this. I'm very humbled. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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