Invert first and last names

lcarlson

New Member
Joined
Jan 19, 2005
Messages
4
Quick questions: What vba code is used with command button to invert first and last names in an excel spreadsheet cell?

Larry
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
You don't really need VBA code, it could be done with worksheet functions (MID, LEFT, FIND etc)

The actual code/functions to use would depend on the format the name is stored in.

How are the first name and last name seperated?
 

lcarlson

New Member
Joined
Jan 19, 2005
Messages
4
Thanks for the reply. Yes, I am sure that I can just use a formula using the terms MID, LEFT, TRIM etc. However, there's the rub. I am not able to figure out the exact formula. If you could relate the exact formula I would be very grateful.


Larry
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web

ADVERTISEMENT

Try this ...

As originally provided by Nathan Oliver

=RIGHT(A1,LEN(A1)-FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-MIN(SUMPRODUCT((LEN(A1)-LEN(SUBSTITUTE(A1,{"Jr","III","IV","V","VI"},"")))),1))))&", "&SUBSTITUTE(A1," " &RIGHT(A1,LEN(A1)-FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-MIN(SUMPRODUCT((LEN(A1)-LEN(SUBSTITUTE(A1,{"Jr","III","IV","V","VI"},"")))),1)))),"")

This will handle most any name you can throw at it. Found here.
 

lcarlson

New Member
Joined
Jan 19, 2005
Messages
4

ADVERTISEMENT

Zack,

That's great. Just what I wanted.

One minor thing. Is it possible to put this formula into an If
statement (such as =IF(ISBLANK(A1), "", formula for inverting names).

In actual practice, I get a #Value message whenever there is a blank
in the list of names in column A1.

A minor refinement, but I was just curious as to whether it was possible.
I tried for a while, with no luck.

Larry
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
Larry

I know a solution has already been provided but a simpler one might be just something like this:

=IF(ISBLANK(A1),"",MID(A1,FIND(" ",A1)+1,LEN(A1))&" ,"&LEFT(A1,FIND(" ",A1)-1))

This might also deal with your 2nd question.
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
If you wanted to use the formula I posted, you could have it in an intermediary column, possibly hidden (your discrepancy) and use it something like this ...
Book24
ABCD
1JohnDoeSrDoeSr,JohnDoeSr,John
2#VALUE! 
Sheet1


Column B above would be hidden, then the formula in column C is ..

=IF(ISERR(B1),"",B1)
copy down as needed.

I also added a "Sr" into the function, as it didn't have it in the prior posting. Which would make it ...

=RIGHT(A1,LEN(A1)-FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-MIN(SUMPRODUCT((LEN(A1)-LEN(SUBSTITUTE(A1,{"Sr","Jr","III","IV","V","VI"},"")))),1))))&", "&SUBSTITUTE(A1," " &RIGHT(A1,LEN(A1)-FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-MIN(SUMPRODUCT((LEN(A1)-LEN(SUBSTITUTE(A1,{"Sr","Jr","III","IV","V","VI"},"")))),1)))),"")

You could also break this formula up into more than one cell in other ways, but the error check is my personal preference.

If you do not need the extreme flexibility of this solution, maybe the one Norie posted would suffice for you. Remember, if you only drive a Volvo, there's no (practical) need to gear it up as a Rolls Royce. Unless you are from Texas and like everything big. (j/k) ;)
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
How about:

=IF(ISBLANK(A1),"",REPLACE(A1,1,FIND(" ",A1),"") & ", " & LEFT(A1,FIND(" ",A1)-1))
Book2
ABCD
1JohnDoeSr.DoeSr.,John
2JoeBrownBrown,Joe
3 
Sheet1
 

Forum statistics

Threads
1,148,050
Messages
5,744,513
Members
423,881
Latest member
Nguyen Vu

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
Top