Split text in column using second space from left

Leonardo

Board Regular
Joined
Nov 8, 2011
Messages
57
Is there a convenient way to split text in to two columns using the second space from the left. The text is not fixed length. There are no potential delimiters other than space characters.
I am using Excel 2016 on a Windows 10.
Thank you
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,166
Office Version
  1. 2016
Hi Leonardo,

https://support.office.com/en-us/ar...s-Wizard-30B14928-5550-41F5-97CA-7A3E9C363ED7


Or you can use those two UDF

Code:
Function SplitText(ByVal strText As String) As String
    Dim intPos As Integer
    
    intPos = InStrRev(strText, " ")
    SplitText = Mid(strText, intPos + 1)
    
End Function

Code:
Function GetFirstPartString(ByVal strText As String) As String
    Dim intPos As Integer
    
    intPos = InStrRev(strText, " ")
    GetFirstPartString = Left(strText, intPos - 1)
    
End Function

HTH
 
Last edited:

sandy666

Banned - Rules violations
Joined
Oct 24, 2015
Messages
7,497
you can try PowerQuery aka Get&Transform

Code:
[SIZE=1]let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Inserted Text Before Delimiter" = Table.AddColumn(Source, "Text Before Delimiter", each Text.BeforeDelimiter([raw data], " ", 1), type text),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Inserted Text Before Delimiter", "Text After Delimiter", each Text.AfterDelimiter([raw data], " ", 1), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Text After Delimiter",{"raw data"})
in
    #"Removed Columns"[/SIZE]
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,443
Office Version
  1. 365
Platform
  1. Windows
Another option
Code:
Sub leonardo()
   Dim Cl As Range
   
   For Each Cl In Range("A2", Range("A" & Rows.count).End(xlUp))
      On Error Resume Next
      Cl.Offset(, 1) = Trim(Mid(Cl, InStr(InStr(1, Cl, " ") + 1, Cl, " ")))
      On Error GoTo 0
      Cl = Trim(Replace(Cl, Cl.Offset(, 1), ""))
   Next Cl
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,443
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Or without a loop
Code:
Sub Leonardo2()
   With Range("A2", Range("A" & Rows.count).End(xlUp))
      .Offset(, 1).Value = Evaluate(Replace("iferror(right(@,len(@)-find(""|"",substitute(@,"" "",""|"",2))),"""")", "@", .Address))
      .Value = Evaluate("if({1},substitute(" & .Address & "," & .Offset(, 1).Address & ",""""))")
   End With
End Sub
 

Leonardo

Board Regular
Joined
Nov 8, 2011
Messages
57
Thank you Silentwolf, sandy66 and Fluff for your replies. I'm only marginally familiar with macros and I needed to get this worksheet for tomorrow morning. I did a brute force parse using text to columns and FlashFill. That is not adequate for the long term and I will review your macros and see if I can figure out how to implement one of them. I'm sure I will have more questions as I try to work with the macros.

Thanks again,

Leonardo
Hi Leonardo,

https://support.office.com/en-us/ar...s-Wizard-30B14928-5550-41F5-97CA-7A3E9C363ED7


Or you can use those two UDF

Code:
Function SplitText(ByVal strText As String) As String
    Dim intPos As Integer
    
    intPos = InStrRev(strText, " ")
    SplitText = Mid(strText, intPos + 1)
    
End Function

Code:
Function GetFirstPartString(ByVal strText As String) As String
    Dim intPos As Integer
    
    intPos = InStrRev(strText, " ")
    GetFirstPartString = Left(strText, intPos - 1)
    
End Function

HTH
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,893
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Or without a loop
Code:
Sub Leonardo2()
   With Range("A2", Range("A" & Rows.count).End(xlUp))
      .Offset(, 1).Value = Evaluate(Replace("iferror(right(@,len(@)-find(""|"",substitute(@,"" "",""|"",2))),"""")", "@", .Address))
      .Value = Evaluate("if({1},substitute(" & .Address & "," & .Offset(, 1).Address & ",""""))")
   End With
End Sub
Here is another way to write a loopless macro for this question...
Code:
[table="width: 500"]
[tr]
	[td]Sub Leonardo2()
  Application.ScreenUpdating = False
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("IF({1},SUBSTITUTE(" & .Address & ","" "",CHAR(1),2))")
    .TextToColumns , xlDelimited, , , False, False, False, False, True, Chr(1)
  End With
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,652
Messages
5,838,591
Members
430,557
Latest member
MK15

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
Top