Split cell contents by line break and put into new rows

chive90

Board Regular
Joined
May 3, 2023
Messages
58
Office Version
  1. 2016
In Column Z I have some rows with multiple lines of data within a cell, and some with just a single line of data in a cell.

For those where there are multiple lines of data (separated by a line break), I would like these inserted as new rows below the cell in question.

I would also like data from Columns P and R copied down from the row that has multiple lines of data. The other columns for the newly inserted rows can remain blank.

This is how it is currently formatted:

Column PColumn RColumn Z
greenjanuarytest1
yellowaugusttest1
test2
test3
purplejunetest4

This is how I would like it to be formatted:

Column PColumn RColumn Z
greenjanuarytest1
yellowaugusttest1
yellowaugusttest2
yellowaugusttest3
purplejunetest4


Is it possible to achieve this?

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
May be something like this,
VBA Code:
Sub CopyUnmergeFillTable()
    Dim ws          As Worksheet
    Dim sourceRange As Range, copiedRange As Range
    Dim lastRowP    As Long, lastRowR As Long, lastRowZ As Long, lastRow As Long
    Dim col         As Range
    Dim i           As Long
    
    Set ws = ActiveSheet
    
    lastRowP = ws.Cells(ws.Rows.Count, "P").End(xlUp).Row
    lastRowR = ws.Cells(ws.Rows.Count, "R").End(xlUp).Row
    lastRowZ = ws.Cells(ws.Rows.Count, "Z").End(xlUp).Row
    
    lastRow = Application.Max(lastRowP, lastRowR, lastRowZ)
    
    Set sourceRange = ws.Range("P1:Z" & lastRow)
    
    lastRow = lastRow + 3
    
    sourceRange.Copy ws.Range("P" & lastRow)
    
    Set copiedRange = ws.Range("P" & lastRow & ":Z" & (lastRow + sourceRange.Rows.Count - 1))
    copiedRange.UnMerge
    
    For Each col In copiedRange.Columns
        For i = 2 To copiedRange.Rows.Count
            If IsEmpty(col.Cells(i, 1).Value) Then
                col.Cells(i, 1).Value = col.Cells(i - 1, 1).Value
            End If
        Next i
    Next col
    
    MsgBox "Table copied, unmerged, And filled down While keeping the original unchanged!", vbInformation
End Sub
 
Upvote 0
@Sam_D_Ben I don't think the cells are merged, they are just single cells & some in col Z have multiple lines.
 
Upvote 0
@Sam_D_Ben I don't think the cells are merged, they are just single cells & some in col Z have multiple lines.
This is what i am seeing when i paste it on xl, and the result below, Also i added some extra rows just incase.
Book1
PQRSTUVWXYZ
1Column PColumn RColumn Z
2greenjanuarytest1
3yellowaugusttest1
4test2
5test3
6purplejunetest4
7greenjanuarytest1
8yellowaugusttest1
9test2
10test3
11purplejunetest4
12
13
14Column PColumn RColumn Z
15greenjanuarytest1
16yellowaugusttest1
17yellowaugusttest2
18yellowaugusttest3
19purplejunetest4
20greenjanuarytest1
21yellowaugusttest1
22yellowaugusttest2
23yellowaugusttest3
24purplejunetest4
Sheet1
 
Upvote 0
That's just how Excel interprets the data. The OP clearly says

Which is not in the data you posted.
Oh! You mean column Z3 has also been merged. correct? Or I am confused with the word <"multipe lines">
 
Upvote 0
Oh! You mean column Z3 has also been merged. correct? Or I am confused with the word <"multipe lines">

"multiple line" in a cell mean
test1--> Alt+Enter --> test2 --> Alt+Enter --> test3 --> Alt+Enter
Cell z3 is made with Alt+Enter
 
Upvote 0
Give this a try with a copy of your data.

VBA Code:
Sub Rearrange()
  Dim Bits As Variant
  Dim r As Long, n As Long
  
  Application.ScreenUpdating = False
  For r = Range("Z" & Rows.Count).End(xlUp).Row To 2 Step -1
    Bits = Split(Range("Z" & r).Value, Chr(10))
      For n = UBound(Bits) To 1 Step -1
        Rows(r + 1).Insert
        Range("P" & r + 1).Value = Range("P" & r).Value
        Range("R" & r + 1).Value = Range("R" & r).Value
        Range("Z" & r + 1).Value = Bits(n)
      Next n
      Range("Z" & r).Value = Bits(0)
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here another option

VBA Code:
Sub split_cell()
  Dim a As Variant, b As Variant, nLines As Variant, itm As Variant
  Dim i As Long, nMax As Long, k As Long
  
  a = Range("P2", Range("Z" & Rows.Count).End(3)).Value
  For i = 1 To UBound(a)
    nMax = nMax + (Len(a(i, 11)) - Len(Replace(a(i, 11), Chr(10), ""))) + 1
  Next
  ReDim b(1 To nMax, 1 To UBound(a, 2))
  
  For i = 1 To UBound(a)
    nLines = Split(a(i, 11), Chr(10))
    For Each itm In nLines
      k = k + 1
      b(k, 1) = a(i, 1)
      b(k, 3) = a(i, 3)
      b(k, 11) = itm
    Next
  Next

  Range("P2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,516
Messages
6,191,500
Members
453,660
Latest member
Wp1902

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