split "firstname lastname" into two cells

bigdan

Well-known Member
Joined
Oct 5, 2009
Messages
846
Office Version
  1. 2013
Platform
  1. Windows
I have a spreadsheet. Column A has the first name then last name, with just a space in the middle. I want to make these into 2 columns. How?

Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
this puts first name in column A and last in Column B

Code:
Sub split_em_up()
     For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        SpacePlace = Application.WorksheetFunction.Search(" ", Cells(i, 1))
        FirstName = Left(Cells(i, 1), SpacePlace)
        LastName = Right(Cells(i, 1), SpacePlace)
        Cells(i, 1) = FirstName
        Cells(i, 2) = LastName
     Next i
End Sub
 
Upvote 0
bigdan,


Sample data before the macro:


Excel Workbook
AB
1Name
2big dan
3hiker 95
4
Sheet1





After the macro:


Excel Workbook
AB
1Name
2bigdan
3hiker95
4
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub SplitName()
' hiker95, 04/11/2011
' http://www.mrexcel.com/forum/showthread.php?t=542776
Dim c As Range, SP
For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
  SP = Split(c, " ")
  c = SP(0)
  c.Offset(, 1) = SP(1)
Next c
End Sub


Then run the SplitName macro.
 
Upvote 0
Why not just use Excel's native "Text to Columns" function? It's what it's there for!
 
Upvote 0
I have a spreadsheet. Column A has the first name then last name, with just a space in the middle. I want to make these into 2 columns. How?

Thanks!
If the first names are always one word names...

Select the cells in question
Goto Data>Text to Columns
Delimited
Next
Select: Space
Finish
 
Upvote 0
If the first names are always one word names...

Select the cells in question
Goto Data>Text to Columns
Delimited
Next
Select: Space
Finish

Well... YEAH, if you wanted to do it the EASY way.... But coding is like, more fun and stuff.
 
Upvote 0
wow you guys are so helpful - 5 responses in 12 mins!

im heading out of work right now, will look at these first thing in the morning. thanks so much guys!
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top