Macro to break quantiy for 1,2,3 etc to single lines

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,
but of a problem,

I've asked my supplier for product codes and they sent me a list in excel

its in sheet"data1"
Row 1 is headers
the data is in columns A:BZ
now the problem i have is some products are sold in Quantiys and some are not,

Column G has the quanity in it. for singlr items this is blank so the row is fine
But for multipacks i get this in column G "1,2,3,4,5,6" or "12,24,35" it can be any number seperated by a comma everytime.

I need amacro to copy the data from sheet Data1 to sheet Data2
if the row has nothing in column G then just copy it once, if it has data then i need it split up into multiple rows so each row only has 1 of the numbers.

Please help if you can

Thanks

Tony
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try:
Code:
Sub CopyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, qty As Range, splitQty As Variant, i As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Data1")
    Set desWS = Sheets("Data2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each qty In srcWS.Range("G2:G" & LastRow)
        If qty = "" Then
            qty.EntireRow.Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
        Else
            splitQty = Split(qty, ",")
            For i = LBound(splitQty) To UBound(splitQty)
                srcWS.Rows(qty.Row).EntireRow.Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1, 0)
                desWS.Range("G" & desWS.Range("G" & desWS.Rows.Count).End(xlUp).Row) = splitQty(i)
            Next i
        End If
    Next qty
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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