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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this
VBA Code:
Sub ReverseName()
    Dim myRange As Range, myCell As Range, NameList As Variant, nStr As String, c As Long
    Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Application.Selection.Address, Type:=8)
    For Each myCell In myRange
        NameList = VBA.Split(WorksheetFunction.Trim(myCell.Value))
        If UBound(NameList) = 1 Then
            myCell.Value = NameList(1) & " " & NameList(0)
        ElseIf UBound(NameList) > 1 Then
            If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
                For c = 2 To UBound(NameList)
                    nStr = nStr & " " & NameList(c)
                Next c
                myCell.Value = NameList(1) & " " & NameList(0) & nStr
            End If
        End If
    Next
End Sub

NOTES
The default value for Split delimeter is space, so does not require specifying explicitly
- code amended
Amended your code to ignore double spaces
Variable declarations inserted
 
Upvote 0
Hi Yongle
A couple of comments
  • With your structure, nStr needs to be reset to a null string for each myCell else it will keep increasing in size
  • You can limit the number of resulting terms in a Split so there is no need to manually re-join terms 2, 3, etc

My suggestion
VBA Code:
Sub ReverseName_v2()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
  
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  For Each myCell In myRange
    If InStr(myCell.Value, " ") > 0 Then
      If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
        NameList = Split(myCell.Value & " ", , 3)
        myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
      End If
    End If
  Next
End Sub
 
Upvote 0
Solution
Hi Yongle
A couple of comments
  • With your structure, nStr needs to be reset to a null string for each myCell else it will keep increasing in size
  • You can limit the number of resulting terms in a Split so there is no need to manually re-join terms 2, 3, etc

My suggestion
VBA Code:
Sub ReverseName_v2()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
 
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  For Each myCell In myRange
    If InStr(myCell.Value, " ") > 0 Then
      If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
        NameList = Split(myCell.Value & " ", , 3)
        myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
      End If
    End If
  Next
End Sub
Nice. I've never used the Limit parameter of the Split Function. How would you handle the error occurring when the user presses Cancel?
I was thinking:

VBA Code:
On Error Resume Next
Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
If Err.Number = 424 Then
   Exit Sub
EndIf
On Error GoTo 0

Is there a 'cleaner' way?
 
Upvote 0
Thank you to both of you, it was spot on!
 
Upvote 0
How would you handle the error occurring when the user presses Cancel?
Good point and your suggestion should be fine. I generally prefer to keep a single exit point for the sub so I would have gone with

VBA Code:
Sub ReverseName_v3()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
  
  On Error Resume Next
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  On Error GoTo 0
  If Not myRange Is Nothing Then
    For Each myCell In myRange
      If InStr(myCell.Value, " ") > 0 Then
        If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
          NameList = Split(myCell.Value & " ", , 3)
          myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
        End If
      End If
    Next myCell
  End If
End Sub
 
Upvote 0
Good point and your suggestion should be fine. I generally prefer to keep a single exit point for the sub so I would have gone with

VBA Code:
Sub ReverseName_v3()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
 
  On Error Resume Next
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  On Error GoTo 0
  If Not myRange Is Nothing Then
    For Each myCell In myRange
      If InStr(myCell.Value, " ") > 0 Then
        If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
          NameList = Split(myCell.Value & " ", , 3)
          myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
        End If
      End If
    Next myCell
  End If
End Sub
Brilliant. I definitely like the single exit point strategy. Thanks a lot.
 
Upvote 0
Good point and your suggestion should be fine. I generally prefer to keep a single exit point for the sub so I would have gone with

VBA Code:
Sub ReverseName_v3()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
 
  On Error Resume Next
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  On Error GoTo 0
  If Not myRange Is Nothing Then
    For Each myCell In myRange
      If InStr(myCell.Value, " ") > 0 Then
        If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
          NameList = Split(myCell.Value & " ", , 3)
          myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
        End If
      End If
    Next myCell
  End If
End Sub
[QUOTE="Peter_SSs, post: 5584183, member: 44226"]
Good point and your suggestion should be fine. I generally prefer to keep a single exit point for the sub so I would have gone with

[CODE=vba]
Sub ReverseName_v3()
  Dim myRange As Range, myCell As Range
  Dim NameList As Variant
 
  On Error Resume Next
  Set myRange = Application.InputBox("Select one Range that you want to reverse name", "ReverseName", Selection.Address, Type:=8)
  On Error GoTo 0
  If Not myRange Is Nothing Then
    For Each myCell In myRange
      If InStr(myCell.Value, " ") > 0 Then
        If MsgBox("switch first 2 words?" & vbCr & myCell.Value, vbYesNo, "") = vbYes Then
          NameList = Split(myCell.Value & " ", , 3)
          myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
        End If
      End If
    Next myCell
  End If
End Sub
[/COD
[/QUOTE]
[/QUOTE]

Sir, I noticed that one of my previous VBA interrupts your VBA at  [CODE=vba]myCell.Value = Trim(Join(Array(NameList(1), NameList(0), NameList(2))))
. My VBA is to remove all extra spaces within the cells. Is this an easy fix? It's so annoying for my VBA works fine, but apparently i can't keep it if I want you. Is there a way around this?
VBA Code:
Sub Trim()
Dim cell As Range
For Each cell In Selection
  cell = Application.Trim(cell)
Next cell
End Sub
 
Upvote 0
@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)?
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,846
Members
449,194
Latest member
HellScout

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