vba to reorder last,first name in same cell

lwebbie

New Member
Joined
Feb 28, 2017
Messages
24
I have a spreadsheet that the names are formatted last,first (starting in cell A2) but I need the order changed. I know the excel formula but that requires me to enter it into an empty cell. Is it possible in VBA to have it switch the order (first last) in the same cell (A2) so it just changes the cell value rather than having to apply the formula to a different cell and then override the data in column A?

Excel formula: MID(A2&" "&A2,FIND(",",A2)+1,LEN(A2))
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this with a copy of your data.
Code:
Sub SwapNames()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",mid(#&"" ""&#,find("","",#)+1,len(#)))", "#", .Address))
  End With
End Sub
 
Upvote 0
Peter it almost worked but now there is an extra space at the start of the name. Is there a way to fix so I don't also have to do a TRIM?
 
Upvote 0
An alternative;

Code:
Sub Test()    NoA = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To NoA
        Range("A" & i) = Trim(StrReverse(Split(StrReverse(Range("A" & i)), ",")(0)) & ", " _
                       & StrReverse(Split(StrReverse(Range("A" & i)), ",")(1)))
    Next
End Sub
 
Upvote 0
this switches the order but what I need is it to remove the , so it would go from Doe, Jane to Jane Doe
 
Upvote 0
Peter, never mind this worked perfectly! I did it in a practice workbook first and forgot my name are formatted without a space. Reran it and it worked like a charm. :) THANK YOU!
 
Upvote 0
Peter, never mind this worked perfectly! I did it in a practice workbook first and forgot my name are formatted without a space. Reran it and it worked like a charm. :) THANK YOU!
No worries, I did exactly the same thing when I tested your original formula. :biggrin:
 
Upvote 0
this switches the order but what I need is it to remove the , so it would go from Doe, Jane to Jane Doe

You could easily remove the comma sign from my code, which comes as;

Code:
Sub Test()NoA = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To NoA
        Range("A" & i) = Trim(StrReverse(Split(StrReverse(Range("A" & i)), ",")(0)) & " " _
                       & StrReverse(Split(StrReverse(Range("A" & i)), ",")(1)))
    Next
End Sub
 
Upvote 0
You could easily remove the comma sign from my code, which comes as;

Code:
Sub Test()NoA = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To NoA
        Range("A" & i) = Trim(StrReverse(Split(StrReverse(Range("A" & i)), ",")(0)) & " " _
                       & StrReverse(Split(StrReverse(Range("A" & i)), ",")(1)))
    Next
End Sub
Notes:
1. This code will error if there are any blank cells in the range (or any cells not containing a comma)
2. If a looping method was desired over the "whole column at once suggestion in post #2 , then a much simpler string manipulation (including a check for note 1) would be:
Code:
Sub Test2()
  NoA = Range("A" & Rows.Count).End(xlUp).Row
  For i = 2 To NoA
    With Range("A" & i)
      If InStr(1, .Value, ",") > 0 Then .Value = Split(.Value & " " & .Value, ",")(1)
    End With
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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