Split rows into new rows

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
How would I go about splitting the following sheet into the second sheet. Im looking for column d to contain a |
if it does, split column by commas and create new rows.

Input example
titlecolordaysizeowner
fordbluemon10john
chevredtue8.1|8.2, 8.3joe

Output example
titlecolordaysizeowner
fordbluemon10john
chevredtue8.1|8.2,joe
chevredtue8.3joe

thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Easily done with Power Query.
Add custom column and get text after delimiter pipe. Then Split column by delimiter coma and split to rows.
Google it for a step by step tutorial
 
Upvote 0
Are there always only 2 size groups max in column D or could there be more, each group separated by a comma?
 
Upvote 0
Are there always only 2 size groups max in column D or could there be more, each group separated by a comma?
There can be many many more. In this example, i show only two

Thanks
 
Upvote 0
Try this with a copy of your workbook.

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant, Sizes As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba2 As Long
  
  With Range("A1", Range("E" & Rows.Count).End(xlUp))
    a = .Value
    uba2 = UBound(a, 2)
    ReDim b(1 To Rows.Count, 1 To uba2)
    For i = 1 To UBound(a)
      r = r + 1
      For j = 1 To uba2
        b(r, j) = a(i, j)
      Next j
      Sizes = Split(a(i, 4), ",")
      If UBound(Sizes) > 0 Then
        b(r, 4) = Sizes(0)
        For k = 1 To UBound(Sizes)
          r = r + 1
          For j = 1 To uba2
            b(r, j) = a(i, j)
          Next j
          b(r, 4) = Sizes(k)
        Next k
      End If
    Next i
    .Offset(, .Columns.Count + 1).Resize(r).Value = b
  End With
End Sub

Here is my sample data and results of the code.

Danny54.xlsm
ABCDEFGHIJK
1titlecolordaysizeownertitlecolordaysizeowner
2fordbluemon10johnfordbluemon10john
3chevredtue8.1|8.2, 8.3joechevredtue8.1|8.2joe
4chevredtue8.3joe
5
Sheet1
 
Upvote 0
Solution
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,679
Messages
6,126,177
Members
449,296
Latest member
tinneytwin

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