Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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.
by Allen Wyatt
(last updated October 3, 2015)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11399) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Reversing Names In Place.
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!
Developing macros can be rewarding, but it can also be challenging. Getting individual macros to run properly is hard ...Discover More
Open up a workbook, and Excel normally runs the macros associated with that workbook. You can disable the automatic ...Discover More
One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.