How to rearrange text in a cell in Excel?
Suppose you have a spreadsheet with a long list of names formatted as “Last name First name”, and now you need to rearrange all names to “First name, Last name”. How can you achieve this? This article introduces three methods to solve the problem.
Rearrange text in a cell with formula
Rearrange text in a cell with VBA code
Quickly rearrange text in a cell with Kutools for Excel
Rearrange text in a cell with formula
The below formula can help you flip last name and first name in a cell in Excel. Please do as follows.
1. Select a blank cell to output the rearranged text, enter the below formula into it and then press the Enter key.
=TRIM(MID(b2,SEARCH(" ",b2)+1,250))&" "&LEFT(b2,SEARCH(" ",b2)-1)
Note: in the formula, B2 is the cell contains the name you will rearrange. Please change it based on your need.
2. Keep selecting the result cell, drag the Fill Handle down to flip all names as below screenshot shown.
Easily rearrange text in a cell in Excel:
The Reverse Text utility of Kutools for Excel can help you to easily swap text around within one cell by certain separator in Excel. See below screenshot:
Kutools for Excel - Supercharge Excel with over 300 essential tools. Enjoy permanently free AI features! Get It Now
Rearrange text in a cell with VBA code
Besides, you can apply below VBA code to flip all names in a list at once in Excel. Please do as follows.
1. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, click Insert > Module. Then copy below VAB code into the Module window.
VBA code: Rearrange text in a cell
Sub RearrangeText()
'Updated by Extendoffice 20180503
Dim xRg As Range, yRg As Range
Dim LastRow As Long, i As Long
Dim strTxt As String, strFs As String
Dim strLs As String, N As Integer
On Error Resume Next
Set xRg = Application.InputBox(Prompt:="Range Selection:", _
Title:="Kutools for excel", Type:=8)
For Each yRg In xRg
On Error Resume Next
strTxt = yRg.Value
Trim (strTxt)
N = InStr(strTxt, " ")
strLs = Left(strTxt, N - 1)
strFs = Right(strTxt, Len(strTxt) - N)
yRg.Value = strFs & " " & strLs
Next
End Sub
3. Then a Kutools for Excel dialog box pops up, please select the name list you want to rearrange, and then click the OK button. See screenshot:
Then all “Last name First name” entries in the selected list are flipped immediately.
Quickly rearrange text in a cell with Kutools for Excel
This section will introduce the Reverse Text Order utility of Kutools for Excel to help you rearrange text in a cell.
1. Select the name list you will flip the “Last name First name”, then click Kutools > Text > Reverse Text Order. See screenshot:
2. In the Reverse Text dialog box, select the Space option and then click the OK button. See screenshot:
Then all names in selected list are rearranged at once.
Kutools for Excel - Supercharge Excel with over 300 essential tools. Enjoy permanently free AI features! Get It Now
Related articles:
- How to swap or reverse day and month of date in Excel?
- How to swap or switch text around within one cell in Excel?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!