VBA Text to column first space only

Joey_Ng

New Member
Joined
Mar 7, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi,

hoping someone could help me with the vba code for text to column but split using the first space only. Eg, John Sam Smith, I want to split using only the first space after John, ignoring the second space (and other spaces for extra text), eg result: "John" and "Sam Smith"
below code split using all spaces:
Selection.TextToColumns DataType:=xlDelimited, Space:=True

Thanks,

Joey
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Can be achieved with Power Query in one step. Post back with sample data if interested in this type of solution.
 
Upvote 0
VBA Code:
Sub test()
     Selection.Name = "Bart" 'named range
     Selection.Offset(, 1).Value = [LEFT(bart&" ",SEARCH(" ",bart&" ")-1)]
End Sub
 
Upvote 0
Try this with a copy of your data.

VBA Code:
Sub SplitFirstSpace()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("substitute(" & .Address & ","" "","";"",1)")
    .TextToColumns DataType:=xlDelimited, Semicolon:=True, Space:=False, Other:=False
  End With
End Sub

Before

Joey_Ng.xlsm
AB
1Name Last Name
2John Sam Smith
3Ted Jones
4
5Bob Bill Joe Hill
6Sam
7Ken Tom Hall
Sheet1


After

Joey_Ng.xlsm
AB
1NameLast Name
2JohnSam Smith
3TedJones
4
5BobBill Joe Hill
6Sam
7KenTom Hall
Sheet1
 
Last edited:
Upvote 0
Solution
Another option, because it's nice to have choices ;) This one keeps the first name if that's all there is.

VBA Code:
Sub SplitFirstSpaceOnly()
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        Range(Cells(i, 1), Cells(i, 2)).Value = Split(Cells(i, 1).Value, " ", 2)
    Next i
End Sub

Before
personal.xlsb
AB
1John Sam Smith
2John Sam Smith
3John Smith
4
5John Sam Smith
6John
7John Sam Smith
Sheet1


After
personal.xlsb
AB
1JohnSam Smith
2JohnSam Smith
3JohnSmith
4
5JohnSam Smith
6JohnJohn
7JohnSam Smith
Sheet1
 
Upvote 0
This one keeps the first name if that's all there is.
I thought that is what mine did. To me, yours actually introduces a new second name.

Consider the result of your code after starting with these two different rows of data.

Joey_Ng.xlsm
A
1Anthony Anthony
2Anthony
Sheet2
 
Upvote 0
I thought that is what mine did. To me, yours actually introduces a new second name.

Consider the result of your code after starting with these two different rows of data.

Joey_Ng.xlsm
A
1Anthony Anthony
2Anthony
Sheet2

Point taken Peter. Perhaps this is closer to what the OP wants?

VBA Code:
Sub SplitFirstSpaceOnly()
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        If InStr(1, Trim(Cells(i, 1)), " ") > 0 Then
            Range(Cells(i, 1), Cells(i, 2)).Value = Split(Cells(i, 1).Value, " ", 2)
        End If
    Next i
End Sub

Before
personal.xlsb
AB
1Anthony Anthony
2Anthony
3John Smith
4
5John Sam Smith
6John
7John Sam Smith
Sheet1


After
personal.xlsb
AB
1AnthonyAnthony
2Anthony
3JohnSmith
4
5JohnSam Smith
6John
7JohnSam Smith
Sheet1
 
Upvote 0
.. another way without processing row-by-row

VBA Code:
Sub SplitFirstSpace_v2()
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Offset(, 1).Value = Evaluate(Replace("replace(#,1,find("" "",#&"" ""),"""")", "#", .Address))
    .Value = Evaluate(Replace("left(#,find("" "",#&"" "")-1)", "#", .Address))
  End With
 
Upvote 0
An alternative using the data in Peter's post and applying the following Mcode in Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([Name Last Name] <> null)),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Filtered Rows", "Name Last Name", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Name Last Name.1", "Name Last Name.2"})
in
    #"Split Column by Delimiter"
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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