Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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

by Allen Wyatt
(last updated October 3, 2015)

11

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 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, and 2016. 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

Keyboard Control of the Find and Replace Dialog Box

Hate to take your hands off the keyboard? This tip explains how you can use the keyboard to work with the Find and ...

Discover More

Double Indenting

Indenting a paragraph is easy in Word. In fact, the program provides shortcut keys that make it a snap. Indenting from ...

Discover More

Fast AutoFill

Want to fill a long column with predictive data? It's easy to do by using AutoFill and a double-click of the mouse.

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Finding Positions of Formatted Characters in a Cell

With a little bit of work, Excel allows you to format individual characters of the text you place in a cell. If you want ...

Discover More

Generating Unique Numbers for Worksheets

You may need to automatically generate unique numbers when you create new worksheets in a workbook. Here are a couple of ...

Discover More

Relative References when Recording Macros

When you record a macro, make sure that you know how Excel is recording your cell movements. This tip explains the ...

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}] 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 + 4?

2018-07-28 14:09:29

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


2015-10-14 11:43:31

Michael (Micky) Avidan

@Bryan,
It seems as if your suggested formula can be shorten a little bit:

=MID(C3,FIND(" ",C3)+1,LEN(C3))&" "&LEFT(C3,FIND(",",C3)-1)

-------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL


2015-10-13 13:54:44

Bryan

Left out how to use the formula to do what George wants.
I often make a Tab called Raw
In this I keep all the original data such as George receives from others. Then on my Work or production tab, I use a formula to pull from Raw the data I need. Once I have my starting data correctly, I either run it using the formula or I copy paste/special values to fix it as needed.

Both doing this and using macro's have good points/bad points. Personally, modifying raw data is just asking for Humpty Dumpty to go splat on your data.


2015-10-13 13:46:41

bryan

The macro is modifying the data. If that is what you want, it is a good way to do it. If however, you do not want to modify the underlying data. Then this formula will reverse the names. (It would reside in a different cell / space.)
Assume name is in C3

=TRIM(RIGHT(C3,LEN(C3)-FIND(",",C3)) & " " & LEFT(C3,FIND(",",C3)-1))

* This also removes the comma *


2015-10-06 16:59:00

santo

Thank you Peter, works a treat.


2015-10-06 10:50:47

Peter Atherton`

@Santo & & Jim

Change the line
s = n(UBound(n)) & ","

to
s = n(UBound(n))


2015-10-05 20:57:32

SastryT

Hi,

I use the below macro to reverse a given string of words in the selection. This can be applied not only on names, but any string of words.

Sub Reverse_string_of_words()

Dim Cell As Range
Dim lSpacePosition As Long
Dim sLastName As String
Dim namex As String

For Each Cell In Selection
sLastName = ""
namex = Trim(Cell.Value2)
Do While InStrRev(namex, " ") > 0
lSpacePosition = InStrRev(namex, " ")
sLastName = sLastName & " " & Mid$(namex, lSpacePosition + 1)
namex = Left$(namex, lSpacePosition - 1)
Loop
Cell.Value2 = Trim(sLastName & " " & namex)

Next Cell

End Sub


2015-10-05 17:53:47

santo

I have the same Query as Jim, is it possible to remove the comma's?


2015-10-05 03:21:48

Jim Bullock

Is it possible to remove the commas?


2015-10-04 09:08:53

Locke Garmin

Here is a way to avoid using arrays:

Sub ReverseNames()

Dim Cell As Range
Dim lSpacePosition As Long
Dim sFirstName As String
Dim sLastName As String

For Each Cell In Selection
lSpacePosition = InStrRev(Cell.Value2, " ")

sFirstName = Left$(Cell.Value2, lSpacePosition - 1)
sLastName = Mid$(Cell.Value2, lSpacePosition + 1)

Cell.Value2 = sLastName & ", " & sFirstName
Next Cell

End Sub


2015-10-03 12:06:57

Willy Vanhaelen

Here is a shorter version of the macro:

Sub ReverseNames()
Dim c As Range, n As Variant, s As String
For Each c In Selection
n = Split(c)
s = n(UBound(n)) & ", "
n(UBound(n)) = ""
c.Value = Trim(s & Join(n))
Next c
End Sub

Anyhow both my version and the one in this tip cannot cope with names such as 'Eddie Van Halen' or 'Jean de la Croix'.


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.