Forum Discussion
ajames1450
Oct 28, 2022Copper Contributor
how do i duplicate rows based on a cells value
im trying to get data from one table and add it multiple times to another table based on the max qty product max qty order product qty product 1 6 12 product 1 6 product...
OliverScheurich
Oct 29, 2022Gold Contributor
Sub max_qty()
Dim i As Long
Dim j As Long
Dim m As Long
Dim l As Long
Dim k As Long
Range("E:F").Clear
Cells(1, 5).Value = "product"
Cells(1, 6).Value = "qty"
i = Range("A" & Rows.Count).End(xlUp).Row
k = 2
For j = 2 To i
l = Application.WorksheetFunction.RoundUp((Cells(j, 3).Value / Cells(j, 2).Value), 0)
For m = 1 To l
If m = l Then
Cells(k, 6).Value = Cells(j, 3).Value - (Cells(j, 2).Value * (m - 1))
Cells(k, 5).Value = Cells(j, 1).Value
k = k + 1
Else
Cells(k, 6).Value = Cells(j, 2).Value
Cells(k, 5).Value = Cells(j, 1).Value
k = k + 1
End If
Next m
Next j
End Sub
An alternative could be these lines of code. In the attached file you can click the button in cell H2 to run the macro.