Split Text String by Line Break in Excel

In the previous post, we talked that how to split text string by a specified character in excel. And this post will guide you how to split text string by Line Break character. The difference is that you can type the line break character directly in the FIND function and you have to use the CHAR function to get the line break character.

1. Split Text String by Line Break using Formula

When you want to split text string by line break in excel, you should use a combination of the LEFT, RIGHT, CHAR, LEN and FIND functions. The CHAR (10) function will return the line break character, the FIND function will use the returned value of the CHAR function as the first argument to locate the position of the line break character within the cell B1.

You can refer to the below formula that will find the position of line break character in Cell B1 and extract all the characters to the left of the line break character.

=LEFT(B1,FIND(CHAR(10),B1,1)-1)

Suppose you have a list of student’s height in column B and you want to extract only the name part. You can use the above excel formula in Cell C1.

Split Text String by Line Break in Excel1

If you need to extract all characters to the right of line breaker character within the text string in Cell B1, you can use the LEN and FIND functions within the RIGHT function in excel.  The below is the formula that will find the position of line character in Cell B1 and extract all the characters to the right of it:

=RIGHT(B1, LEN(B1)-FIND(CHAR(10),B1))
Split Text String by Line Break in Excel2

2. Split Text String by Line Break using VBA Code

Moving on to the second method, let’s explore VBA code to dynamically split text strings by line breaks.

Press ‘Alt + F11‘ to open the Visual Basic for Applications (VBA) editor.

In the editor, right-click on any item in the project explorer, choose ‘Insert,’ and select ‘Module‘ to add a new module.

Copy and paste the following modified VBA code into the module:

Sub SplitTextByLineBreaks()
    Dim sourceRange As Range
    Dim destinationCell As Range
    
    ' Prompt user to select the range of source cells
    On Error Resume Next
    Set sourceRange = Application.InputBox("Select the range of cells with the text strings to split", Type:=8)
    On Error GoTo 0
    
    ' Exit if the user cancels the selection
    If sourceRange Is Nothing Then
        Exit Sub
    End If
    
    ' Prompt user to select the starting destination cell for the parts
    On Error Resume Next
    Set destinationCell = Application.InputBox("Select the starting destination cell for the parts", Type:=8)
    On Error GoTo 0
    
    ' Exit if the user cancels the selection
    If destinationCell Is Nothing Then
        Exit Sub
    End If
    
    ' Loop through each cell in the source range
    For Each sourceCell In sourceRange
        ' Split text by line break and return all values in the same row
        Dim textArray() As String
        textArray = Split(sourceCell.Value, Chr(10))
        
        ' Output values to destination cells in the same row as the source cell
        For i = LBound(textArray) To UBound(textArray)
            destinationCell.Offset(sourceCell.Row - sourceRange.Cells(1, 1).Row, i).Value = textArray(i)
        Next i
    Next sourceCell
End Sub

Close the VBA editor by clicking the “X” button or pressing ‘Alt + Q.’

Return to your Excel workbook and press ‘Alt + F8‘ to open the “Macro” dialog box.

In the “Macro” dialog box, choose the macro named “SplitTextByLineBreaks” and click ‘Run’

The macro will prompt you to select the source cell with the text string and the destination cell for the first part. Click on the desired cells.

The text string will be dynamically split, and the first part will be placed in the destination cell. The original cell will now contain the remaining text after the line break.

3. Video: Split Text String by Line Break

This Excel video tutorial where we’ll explore two effective methods for splitting text strings by line breaks. Join us as we uncover a formula-based solution utilizing the LEFT, RIGHT, and FIND functions, followed by a dynamic approach using VBA code.

4. Related Formulas

  • Split Text String by Specified Character in Excel
    you should use the Left function in combination with FIND function to split the text string in excel. And we can refer to the below formula that will find the position of “-“in Cell B1 and extract all the characters to the left of the dash character “-“.=LEFT(B1,FIND(“-“,B1,1)-1).…
  • Split Text String to an Array
    If you want to convert a text string into an array that split each character in text as an element, you can use an excel formula to achieve this result. the below will guide you how to use a combination of the MID function, the ROW function, the INDIRECT function and the LEN function to split a string…
  • Split Multiple Lines from a Cell into Rows
    If you have multiple lines in a cell and each line is separated by line break character or press “alt + enter” key while you entering the text string into cells, and you need to extract the multiple lines into the separated rows or columns, you can use a combination with the TRIM function, the MID function, the SUBSTITUTE function, the REPT function, the LEN function to create a complex excel formula..…
  • Split Text and Numbers
    If you want to split text and numbers, you can run the following excel formula that use the MIN function, FIND function and the LEN function within the LEFT function in excel. And if you want to extract only numbers within string, then you also need to RIGHT function to combine with the above functions..…

5. Related Functions

  • Excel Find function
    The Excel FIND function returns the position of the first text string (substring) from the first character of the second text string.The FIND function is a build-in function in Microsoft Excel and it is categorized as a Text Function.The syntax of the FIND function is as below:= FIND  (find_text, within_text,[start_num])…
  • Excel CHAR function
    The Excel CHAR function returns the character specified by a number (ASCII Value).The CHAR function is a build-in function in Microsoft Excel and it is categorized as a Text Function. The syntax of the CHAR function is as below:=CHAR(number)….
  • Excel LEFT function
    The Excel LEFT function returns a substring (a specified number of the characters) from a text string, starting from the leftmost character.The LEFT function is a build-in function in Microsoft Excel and it is categorized as a Text Function.The syntax of the LEFT function is as below:= LEFT(text,[num_chars])…
  • Excel LEN function
    The Excel LEN function returns the length of a text string (the number of characters in a text string).The LEN function is a build-in function in Microsoft Excel and it is categorized as a Text Function.The syntax of the LEN function is as below:= LEN(text)…
  • Excel RIGHT function
    The Excel RIGHT function returns a substring (a specified number of the characters) from a text string, starting from the rightmost character.The syntax of the RIGHT function is as below:= RIGHT (text,[num_chars])…

Leave a Reply