Switch First & Last Name Separated by Comma and AutoFill

Jones1413

New Member
Joined
Jul 26, 2019
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have names in Column A that are in this order "Jones, Ben" and I want the code to switch the name to say "Ben Jones" in Column B. I have the below VBA and I keep getting a Syntax Error message(see screen shot attached). I then want to AutoFill the formula in Column B to the last row of data in the worksheet. There typically won't be any blank rows in between the data but I included that just as a precaution.

VBA Code:
Sub Macro1()

Range("B1").Select

Dim last_row As Long

last_row = Cells(Rows.Count, 2).End(xlUp).Row

ActiveCell.Formula = "=RIGHT(A1,LEN(A1)-SEARCH(" ",A1))&" "&LEFT(A1,SEARCH(",",A1)-1)"

ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":B" & last_row)

End Sub

Book1
AB
1Jones, BenBen Jones
2Johnson, Robert
3Smith, Gary
4
5
6
7
8
9
10
11Jordan, Michael
Sheet1
Cell Formulas
RangeFormula
B1B1=RIGHT(A1,LEN(A1)-SEARCH(" ",A1))&" "&LEFT(A1,SEARCH(",",A1)-1)
 

Attachments

  • Capture.JPG
    Capture.JPG
    41.2 KB · Views: 17

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If you are fine with formula then try

Book1
ABC
1Jones, BenBen Jones
2Johnson, RobertRobert Johnson
3Smith, GaryGary Smith
4
5
6
7
8
9
10
11Jordan, MichaelMichael Jordan
12
13
14
Sheet1
Cell Formulas
RangeFormula
B1:B1000B1=IFERROR(TEXTAFTER(A1:A1000,", ")&" "&TEXTBEFORE(A1:A1000,","),"")
Dynamic array formulas.
 
Upvote 0
Try this formula:
=RIGHT(A15,LEN(A15)-SEARCH(CHAR(32),A15))& CHAR(32) & LEFT(A15,SEARCH(CHAR(44),A15)-1)
 
Upvote 0
When I type the formula into Excel on it's own, it works fine. It returns the correct output I am looking for: "Jones, Ben" to "Ben Jones"

=RIGHT(A1,LEN(A1)-SEARCH(" ",A1))&" "&LEFT(A1,SEARCH(",",A1)-1)

The problem is when I type it into VBA, I get the error message(see attached). I also get an error on the last line of VBA when it goes to AutoFill the rest of the rows.

I need this to be in VBA, not just in a formula.
 

Attachments

  • Capture.JPG
    Capture.JPG
    41.2 KB · Views: 9
Upvote 0
If you turn on the Macro Recorder and record yourself entering the formula into a cell, you will get the exact VBA code you need for that formula.
 
Upvote 0
That is why I did the following:
Used the CHAR function for spaces and commas. It think VBA may be getting confused with the quotation marks. ""

Also when using the "&" to concatenate in VBA make sure you have spaces on both sides " & ". I have VBA hiccup when I don't put spaces around the &
 
Upvote 0
Why using VBA for standard Excel formula's

Book1
AB
1Jones, BenBen Jones
2Johnson, RobertRobert Johnson
3Smith, GaryGary Smith
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=LET(sp,TEXTSPLIT(A1,", "),TEXTJOIN(" ",,TAKE(sp,,{-1,1})))
 
Last edited:
Upvote 0
That is why I did the following:
Used the CHAR function for spaces and commas. It think VBA may be getting confused with the quotation marks. ""

Also when using the "&" to concatenate in VBA make sure you have spaces on both sides " & ". I have VBA hiccup when I don't put spaces around the &
have you tried the formula in post #3?
 
Upvote 0
Try...
VBA Code:
Sub test()
Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=RIGHT(A1,LEN(A1)-SEARCH("" "",A1))&"" ""&LEFT(A1,SEARCH("","",A1)-1)"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,656
Latest member
earth

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