create Multiple Rows per line item

peedeebee

New Member
Joined
Jun 26, 2018
Messages
8
Hello, I am trying to convert some data currently exported as below:

Order NumberSKUPrice
Order1sku1,sku2$1,$2
Order2sku3,sku4$1,$2

<tbody>
</tbody>

We need this transformed in excel into something like the below:

Order NumberSKUPrice
Order1sku1$1
Order1sku2$2
Order2sku3$1
Order2sku4$2

<tbody>
</tbody>

Any help would be greatly appreciated!

Thanks!
 
Try this:


Excel 2010
ABCDEFGHIJKLMN
1Order NumberOrder Items Product Master SKUQtyPricesShip NameShip CompanyShip Address1Ship Address2Ship Address 3Ship CityShip StateShip Zip CodeShip CountryShip Phone
2MTW1617PPP4,PPI4WPDQ61,211.95,12.95TESTTEST123 Fake StreetUnit 1COLUMBUSOH43214-1586US0
3TEST062718CFK9W,PPP4,PPI4W1,2,311.95,12.95,10.95TESTTEST123 Fake StreetUnit 1Torontoontariom3j2m5CA6479514813
4MTW1617PPP4,PPI4WPDQ61,211.95,12.95TESTTEST123 Fake StreetUnit 1COLUMBUSOH43214-1586US0
Sheet2 (6)


Code:
Sub morerows()
Dim i%, j%, x%, arr As Variant
i = 2
Do Until Cells(i, 1) = ""
x = Len(Cells(i, 2)) - Len(Replace(Cells(i, 2), ",", ""))
If x > 0 Then
ReDim arr(x)
Cells(i + 1, 1).Resize(x).EntireRow.Insert
Cells(i + 1, 1).Resize(x, 14).Value = Cells(i, 1).Resize(, 14).Value
For j = 2 To 4
arr = Split(Cells(i, j), ",")
Cells(i, j).Resize(x + 1) = Application.Transpose(arr)
Next j
Else
End If
i = i + x + 1
Loop
End Sub


Excel 2010
ABCDEFGHIJKLMN
1Order NumberOrder Items Product Master SKUQtyPricesShip NameShip CompanyShip Address1Ship Address2Ship Address 3Ship CityShip StateShip Zip CodeShip CountryShip Phone
2MTW1617PPP4111.95TESTTEST123 Fake StreetUnit 1COLUMBUSOH43214-1586US0
3MTW1617PPI4WPDQ6212.95TESTTEST123 Fake StreetUnit 1COLUMBUSOH43214-1586US0
4TEST062718CFK9W111.95TESTTEST123 Fake StreetUnit 1Torontoontariom3j2m5CA6479514813
5TEST062718PPP4212.95TESTTEST123 Fake StreetUnit 1Torontoontariom3j2m5CA6479514813
6TEST062718PPI4W310.95TESTTEST123 Fake StreetUnit 1Torontoontariom3j2m5CA6479514813
7MTW1617PPP4111.95TESTTEST123 Fake StreetUnit 1COLUMBUSOH43214-1586US0
8MTW1617PPI4WPDQ6212.95TESTTEST123 Fake StreetUnit 1COLUMBUSOH43214-1586US0
Sheet2 (6)
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Awesome! You're the man! Is there some code can be added which will run this every 5 minutes and deposit the formatted data into a new worksheet?
 
Last edited:
Upvote 0
Hi JoeMo - OK I have amended your VBA code to include a 4th column. Here is the sample data:


Book1
ABCD
1Order NumberOrder Items Product Master SKUQtyPrices
2MTW1617PPP4,PPI4WPDQ61, 211.95,12.95
3TEST062718CFK9W,PPP4,PPI4W1, 2, 311.95,12.95,10.95
4MTW1617PPP4,PPI4WPDQ61, 211.95,12.95
5MTW1618PPP4,PPI4WPDQ61, 211.95,12.95
6TEST062718CFK9W,PPP4,PPI4W1,2,429.95,10.95,9.95
7TEST062818CFK9W,PPP4,PPI4W10,5,612.95,9.95,8.95
8MTW1619CFK9W129.95
9TEST0628182CFK9W,PPP4,PKA15,2,429.95,10.95,8.95
UPS_CUSTOMS


Here is the output on the same worksheet:


Book1
FGHI
1Order NumberOrder Items Product Master SKUQtyPrices
2MTW1617PPP4111.95
3MTW1617PPI4WPDQ6212.95
4TEST062718CFK9W111.95
5TEST062718PPP4212.95
6TEST062718PPI4W310.95
7MTW1617PPP4111.95
8MTW1617PPI4WPDQ6212.95
9MTW1618PPP4111.95
10MTW1618PPI4WPDQ6212.95
11TEST062718CFK9W129.95
12TEST062718PPP4210.95
13TEST062718PPI4W49.95
14TEST062818CFK9W1012.95
15TEST062818PPP459.95
16TEST062818PPI4W68.95
17MTW1619CFK9W1
18TEST0628182CFK9W529.95
19TEST0628182PPP4210.95
20TEST0628182PKA148.95
UPS_CUSTOMS


Notice how row 17 is not populating - this is because there was only one price, instead of multiple separated by comma. Is there an IF statement I can add which will still populate the price if it is only singular?

Can I have this post the output into a new worksheet, instead of the same? Is there a way to have this macro run automatically every 5 minutes?

Thank you for all your help!

This is the VBA code I am using:

Code:
Sub RearrangeData()Dim R As Range, c As Range, nxRw As Long, V1 As Variant, V2 As Variant, V3 As Variant
Set R = Range("A1:D" & Cells(Rows.Count, "A").End(xlUp).Row)
Application.ScreenUpdating = False
Range("F:I").ClearContents
With Range("F1:I1")
    .Value = R.Rows(1).Cells.Value
    .EntireColumn.AutoFit
End With
For Each c In R.Offset(1, 0).Resize(R.Rows.Count - 1).Columns(1).Cells
    V1 = Split(c.Offset(0, 1).Value, ",")
    V2 = Split(c.Offset(0, 2).Value, ",")
    V3 = Split(c.Offset(0, 3).Value, ",")
    nxRw = Range("F" & Rows.Count).End(xlUp).Row + 1
    If UBound(V1) > 0 Then
        With Range("F" & nxRw)
            .Resize(UBound(V1) + 1).Value = c.Value
            For i = LBound(V1) To UBound(V1)
                .Offset(i, 1).Value = V1(i)
                .Offset(i, 2).Value = V2(i)
                .Offset(i, 3).Value = V3(i)
            Next i
'            .Offset(0, 2).Resize(UBound(V) + 1).Value = c.Offset(0, 2).Value
        End With
    Else
        Range("F" & nxRw).Resize(1, 3).Value = c.Resize(1, 3).Value
    End If
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here is another macro that you can try...
Code:
[table="width: 500"]
[tr]
	[td]Sub RedistributeData()
  Dim X As Long, LastRow As Long, CellVal As String, Data() As String
  Dim A As Range, Table As Range, Col As Range
  Const Delimiter As String = ","
  Const DelimitedColumn As String = "B:D"
  Const TableColumns As String = "A:N"
  Const StartRow As Long = 2
  Application.ScreenUpdating = False
  LastRow = Columns(TableColumns).Find(What:="*", SearchOrder:=xlRows, _
            SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
  For X = LastRow To StartRow Step -1
    CellVal = Cells(X, Range(DelimitedColumn)(1).Column).Value
    If CellVal Like "*,*" Then
      Intersect(Rows(X + 1), Columns(TableColumns)).Resize(Len(CellVal) - Len(Replace(CellVal, ",", ""))).Insert xlShiftDown
    End If
    For Each Col In Range(DelimitedColumn).Columns
      Data = Split(Cells(X, Col.Column), Delimiter)
      If Len(Cells(X, Col.Column)) Then
        Cells(X, Col.Column).Resize(UBound(Data) + 1) = WorksheetFunction.Transpose(Data)
      End If
    Next
  Next
  LastRow = Cells(Rows.Count, Range(DelimitedColumn).Columns(1).Column).End(xlUp).Row
  On Error Resume Next
  Set Table = Intersect(Columns(TableColumns), Rows(StartRow).Resize(LastRow - StartRow + 1))
  If Err.Number = 0 Then
    Table.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    Columns(DelimitedColumn).SpecialCells(xlFormulas).Clear
    Table.Value = Table.Value
  End If
  On Error GoTo 0
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi JoeMo - OK I have amended your VBA code to include a 4th column. Here is the sample data:




Notice how row 17 is not populating - this is because there was only one price, instead of multiple separated by comma. Is there an IF statement I can add which will still populate the price if it is only singular?

Can I have this post the output into a new worksheet, instead of the same? Is there a way to have this macro run automatically every 5 minutes?

Thank you for all your help!
Your amendment missed a critical line. Now that you have 4 columns of data this bit:

Else
Range("F" & nxRw).Resize(1, 3).Value = c.Resize(1, 3).Value

should have a 4 in place of the red 3.

You can alter the code to put the rearranged data in another sheet. Look into Application.OnTime to run the macro every 5 minutes.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,122
Messages
6,128,958
Members
449,480
Latest member
yesitisasport

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