Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Reversing Names In Place.

Reversing Names In Place

Written by Allen Wyatt (last updated January 22, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


7

George often has to work with data provided by other people. In working with this data he may need to convert a name, say Joe Bloggs, so that the last name is first, as in Bloggs, Joe. George understands that he can use a formula to do the name reversal, but he needs to do it in the same cell in which the name resides. He wonders if there is a built-in command that will perform this task.

No, there isn't a built-in command to do it. You can, however, create a macro that will do the switching for you. This macro could then be assigned to a shortcut key or placed on the toolbar so it can be easily accessed. Here's a simple macro that will do the switching:

Sub ReverseNames()
    Dim c As Range
    Dim n As Variant
    Dim s As String
    Dim j As Integer

    For Each c In Selection
        n = Split(c, " ")
        s = n(UBound(n)) & ","
        For j = LBound(n) To UBound(n) - 1
            s = s & " " & n(j)
        Next j
        c.Value = Trim(s)
    Next c
End Sub

To use the macro, just select the range of cells you want to affect and then run it. The macro separates the text in the cell into individual words (as separated by spaces) and then builds the name back again. It will handle two-word names (such as Joe Bloggs) just fine, but it will also handle longer names (such as Joseph Andrew Bloggs) just as easily.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11399) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Reversing Names In Place.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Odd Behavior when Opening a Shared File with a Shortcut

Tracking down a problem that occurs with a single workbook can be vexing. One such problem occurred with Chris, and these ...

Discover More

Recovered Document becomes Default

Word has a feature called AutoRecover that helps you when Word or Windows crashes. If your Normal template gets messed up ...

Discover More

Blank Lines Before Tables

Adding a blank line before your table is easy, but Word's behavior as you attempt to make the insert can depend on where ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Deleting Blank Rows

Got some pesky blank rows in your data that you want to get rid of? This tip provides a wide variety of methods you can ...

Discover More

Turning Off Screen Updating

Want a quick way to speed up your macros? All you need to do is to stop Excel from updating the screen while the macro is ...

Discover More

Copying a Set Range from Multiple Worksheets to a New Worksheet

Want to create a summary worksheet that pulls a single row of data from each worksheet in the workbook? Here are a couple ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 0 + 1?

2022-01-24 09:59:19

J. Woolley

Sorry. If you copy/paste the first formula in my comment below that begins with =MID(A1,... you must replace the 3 non-breaking space characters with regular space characters.


2022-01-23 12:50:35

J. Woolley

My Excel Toolbox includes the following functions (derived from VBA) that might be useful for manipulating text as described in the Tip:
=FindRev(FindText,WithinText,[StartNum],[CaseSensitive])
=IsLike(Text,Pattern)
=Between(Text,BeginAfter,EndBefore,[CaseSensitive], [Direction])
Assuming a name like "John J. D. Smith" is in cell A1, here is a cell formula that performs like Willy's Reverse() macro:
=MID(A1,FindRev(" ",A1)+1,999)&", "&LEFT(A1,FindRev(" ",A1)-1)

My Excel Toolbox also includes these dynamic array functions:
=SplitText(Text,[Delimiter],[CaseSensitive],[Limit],[Remainder])
=ArraySize(Var)
=GetCols(RangeArray, ParamArray Cols())
Assuming a name like "John J. D. Smith" is in cell A1, here are cell formulas that perform tasks like Peter's SplitRev() macro:
Cell A2 (first name):
=GetCols(SplitText(A1),1)
Cell A3 (last name):
=GetCols(SplitText(A1),ArraySize(SplitText(A1)))
Cell A4 (middle names, if any):
=TRIM(Between(A1,A2,A3))

See https://sites.google.com/view/MyExcelToolbox/


2022-01-23 05:13:33

Peter Atherton

One small drawback with InstrRev is that it only works for two word names. William S. Morris is returned as Morris, William S. The Split method offers a bit more control returning Morris S,, William

Sub SplitRev()
Dim c As Range, x As Variant
For Each c In Selection
x = Split(c, " ")
If UBound(x) = 2 Then
c = x(2) & " " & x(1) & ", " & x(0)
Else
c = x(1) & ", " & x(0)
End If
Next c
End Sub

Mind you it will be slower than Willy's code.


2022-01-23 04:42:05

Willy Vanhaelen

@Jeff
X = InStrRev(Cell, " ") -> X will contain the position of the last space in the string. If X contains 0, meaning there is no space in the string or de cell is blank or contains a number, the following line will be skipped avoiding an error.


2022-01-23 03:12:12

Jeff

Hey Willy, in your code you have 'If X Then Cell'. I don't get it: If X ... what?


2022-01-22 06:18:12

Willy Vanhaelen

This macro, half the size, does the same job:

Sub Reverse()
Dim Cell As Range, X As Integer
For Each Cell In Selection()
    X = InStrRev(Cell, " ")
    If X Then Cell = Mid(Cell, X + 1) & ", " & Left(Cell, X - 1)
Next Cell
End Sub


2022-01-22 05:50:16

Andy

Flash fill is an overlooked feature that will do the name reversal really easily.
Just type what you want the result to be in the cell next to the names.
As you start to type the second name Excel should recognise the pattern and offer to complete the column for you.

This won't do it in the same column as the original data, but a quick cut then paste special values over the original data will accomplish this.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.