How to Split Multiple Lines from a Cell into Rows

This post will guide you how to split multiple lines from a cell into separated rows or columns in Excel. You will learn that how to extract text string separated by line break character into rows in excel 2013.

In the previous post, we talked that how to split text string by only one line break character. And this post will talk that how to split text string by multiple line break character (multiple lines) into rows.

1. 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.

For example, to extract multiple lines in Cell B2, then put it into C2, D2 or E2. You can write down the following excel formula:

=TRIM( MID(SUBSTITUTE( $B2, CHAR(10), REPT( " ",LEN($B2) ) ), (C$1-1)*LEN($B2)+1, LEN($B2)) )

Let’s see how this formula works:

=CHAR(10)

The CHAR function returns the character specified by a number.  If the number is 10, it will return a line break character in text string.

=LEN($B2)

The LEN function returns the total length of text string in Cell B2.

= REPT( ” “,LEN($B2)

This formula will repeat empty string a given number of times that returned by the LEN function.  It will return a new text string.

=SUBSTITUTE( $B2, CHAR(10), REPT( ” “,LEN($B2) ) )

split multiple line from a cell1

The SUBSTITUTE function will replace all line break characters with a new text string returned by the REPT function in a text string in Cell B1. The returned string will go into the MID FUNCTION as its text argument.

= (C$1-1)*LEN($B2)+1

This formula returns the starting position of the first, second, or third or nth substring. The returned value goes into the MID function as its start_num argument.

=MID(SUBSTITUTE( $B2, CHAR(10), REPT( " ",LEN($B2) ) ), (C$1-1)*LEN($B2)+1, LEN($B2))
split multiple line from a cell2

The returned result of the last LEN function is the number of the characters that you want to extract from a text string in Cell B2. It goes into the MID function as its num_chars argument.

The MID function extract a substring from a text string returned by the SUBSTITUTE function based on start_num and num_chars arguments. The returned substring is what you want to extract.

=TRIM()

split multiple line from a cell3

The TRIM function removes all spaces from text string except for single spaces between words.

2. Split Multiple Lines from a Cell into Rows using VBA Code

Now, moving on to the second method, we’ll explore using VBA code to achieve the same task. This method provides more flexibility and customization options for splitting multiple lines from a cell into rows.

Press Alt + F11 on your keyboard. This keyboard shortcut opens the Visual Basic for Applications (VBA) editor.

In the VBA editor window, locate the Project Explorer pane on the left-hand side. If it’s not visible, you can enable it by pressing Ctrl + R.

Right-click on any existing module or within the “Modules” folder (if available) and select Insert, then click  Module. This action inserts a new module into the project.

In the newly created module, copy and paste the following VBA code:

Sub SplitLinesIntoRows()
    Dim sourceRange As Range
    Dim sourceCell As Range
    Dim destCell As Range
    Dim splitText() As String
    Dim i As Integer

    ' Prompt to select source range
    On Error Resume Next
    Set sourceRange = Application.InputBox("Select the range of cells containing the text with multiple lines:", Type:=8)
    On Error GoTo 0

    If sourceRange Is Nothing Then
        MsgBox "No source range selected. Operation aborted."
        Exit Sub
    End If

    ' Prompt to select destination cell
    On Error Resume Next
    Set destCell = Application.InputBox("Select the top-left cell of the destination range where you want to split the lines:", Type:=8)
    On Error GoTo 0

    If destCell Is Nothing Then
        MsgBox "No destination cell selected. Operation aborted."
        Exit Sub
    End If

    Application.ScreenUpdating = False

    For Each sourceCell In sourceRange
        ' Store the value of the source cell
        Dim sourceValue As Variant
        sourceValue = sourceCell.Value
        
        ' Split the text and insert into rows
        splitText = Split(sourceValue, Chr(10))

        ' Place each returned value in one row
        For i = 0 To UBound(splitText)
            destCell.Offset(0, i).Value = splitText(i)
        Next i
        
        Set destCell = destCell.Offset(1, 0) ' Move to the next row for the next source cell
    Next sourceCell

    Application.ScreenUpdating = True
End Sub

Close the VBA editor window to return to your Excel workbook. You can do this by clicking the close button (X) on the VBA editor window or pressing Alt + Q.

Press Alt + F8 on your keyboard. This opens the “Macro” dialog box.

In the “Macro” dialog box, you should see a list of available macros. Select the macro named SplitLinesIntoRows from the list. Click the Run button. This will execute the macro.

After running the macro, you’ll be prompted to select the range of cells containing the text with multiple lines. Click and drag to select the desired range, then click ok button.

Next, you’ll be prompted to select the top-left cell of the destination range where you want to split the lines. Click on the desired cell, then click OK button.

After the macro finishes running, you should see that each returned value for one source cell should now be placed into one row, starting from the specified destination range.

3. Video: Split Multiple Lines from a Cell into Rows

This Excel video tutorial on splitting multiple lines from a cell into rows. In this tutorial, we’ll explore two methods: using a formula with TRIM, MID, and SUBSTITUTE functions, and utilizing VBA code.

https://youtu.be/bALm4ox3fTM

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 Text String by Line Break in Excel
    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.…
  • 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 TRIM function
    The Excel TRIM function removes all spaces from text string except for single spaces between words. The syntax of the TRIM function is as below:=TRIM(text)…
  • Excel MID function
    The Excel MID function returns a substring from a text string at the position that you specify.The syntax of the MID function is as below:= MID (text, start_num, num_chars)…
  • Excel Substitute function
    The Excel SUBSTITUTE function replaces a new text string for an old text string in a text string. The syntax of the SUBSTITUTE function is as below:= SUBSTITUTE  (text, old_text, new_text,[instance_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 REPT function
    The Excel REPT function repeats a text string a specified number of times.The REPT function is a build-in function in Microsoft Excel and it is categorized as a Text Function.The syntax of the REPT function is as below:= REPT  (text, number_times)….
  • 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)…

Leave a Reply