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
 
Rich (BB code):
Evaluate(Replace("replace(#,1,find("" "",#&"" ""),"""")", "#", .Address))
@Peter_SSs , i'm surprised by this construction.
It works, but how does it work ?
Do you have a link or documentation about that "#"
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Do you have a link or documentation about that "#"
The # is simply a placeholder for the range address, and could be any symbol that would not already be in the 'formula part'.

Instead of
Evaluate(Replace("replace(#,1,find("" "",#&"" ""),"""")", "#", .Address))
it could have been written without the # as
Evaluate("replace(" & .Address & ",1,find("" ""," & .Address & "&"" ""),"""")")
but the former has two advantages for me
  1. The bold 'formula part' reads much more like a worksheet formula to me, making the code easier to understand/debug, and
  2. Use of the placeholder makes the code shorter - not a lot here with only two instances of it, but if there are 3, 4, 5 etc then the compacting can be significant.
 
Upvote 0
wow, as i see it now, less spectacular, but a very nice trick !
thanks
 
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
Works great. Thank You!
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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