Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Reversing Names In Place.

Reversing Names In Place

Written by Allen Wyatt (last updated July 28, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003


1

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 a toolbar so it can be easily accessed. Here's a simple macro that will do the switching:

Sub ReverseNames()
    Dim x As Integer
    Dim sCell As String
    Dim sLast As String
    Dim sFirst As String
    Dim rCell As Range

    For Each rCell In Selection
        sCell = rCell.Value
        x = InStr(sCell, " ")
        If x > 0 Then
            sFirst = Left(sCell, x - 1)
            sLast = Mid(sCell, x + 1)
            rCell.Value = sLast & ", " & sFirst
        End If
    Next
    Set rCell = Nothing
End Sub

To use the macro, just select the range of cells you want to affect and then run it. The macro searches for a space within the cell and considers everything in front of the space to be the first name and everything after the space to be the last name. These two elements are reversed, a comma put between them, and stuffed back into the cell.

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 (3812) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Determining a Value of a Cell

Cells can store all sorts of information that can be formatted and displayed in a myriad of ways. If you want to quickly ...

Discover More

Using a Formula to Replace Spaces with Dashes

If you need a formula to change spaces to some other character, the SUBSTITUTE function fits the bill. Here's how to use it.

Discover More

Letters and Numbers in Page Numbers

A common task is to add page numbers to document headers and footers. If you want those page numbers to include more than ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Delimited Text-to-Columns in a Macro

The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...

Discover More

Getting User Input in a Dialog Box

Want to get some input from the users of your workbooks? You can do it by using the InputBox function in a macro.

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

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 2 + 2?

2018-07-28 14:02:34

Rick Rothstein

As long as the selected cells are contiguous, you can use this one-liner to produce the same results as this article's code does...

Sub ReverseNames()

Selection = Evaluate(Replace("IF(@="""","""",MID(@&"", ""&@,FIND("" "",@)+1,LEN(@)+1))", "@", Selection.Address))

End Sub


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.