Duplicate row according to condition

Computerised10

New Member
Joined
Oct 2, 2014
Messages
20
Hi all

I need to duplicate a row according to the value. So if the qty is 1 I don't need to duplicate but if greater than one let's say 5 I need it to duplicate the row 5 times.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
It would be helpful if you provide sample data to show what you require .. ;)

Hi all

I need to duplicate a row according to the value. So if the qty is 1 I don't need to duplicate but if greater than one let's say 5 I need it to duplicate the row 5 times.
 
Upvote 0
Product codedescription Qty
GGHUITELEPHONE g5004
YUIONTABLE3
HIOINCHAIR1

<tbody>
</tbody>

So if qty is 4 duplicate row 4 times. If qty is one do no duplicate. Hope this make it clearer. Thanks
 
Upvote 0
Try this

Code:
Sub k1()
Dim LastRow As Long, i, j, k, LastCol As Integer
k = 0
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    For j = 1 To Sheets("Sheet1").Range("A1").Offset(i - 1, 2)
        k = k + 1
        Sheets("Sheet2").Range("A1").Offset(k - 1, 0) = Sheets("Sheet1").Range("A1").Offset(i - 1, 0)
        Sheets("Sheet2").Range("A1").Offset(k - 1, 1) = Sheets("Sheet1").Range("A1").Offset(i - 1, 1)
    Next j
Next i
End Sub

Output on Sheet2
 
Upvote 0
Try this ... ;)

Code:
'this subroutine duplicates rows if the quantity is > 1
Sub duplicaterow()
    
    Dim lastrow As Long             ' denotes the last row populated with a value in Column C - (Qty)
    Dim qty As Integer              ' denotes the quantity and the no.of times to duplicate if > 1
    
    lastrow = Range("C65536").End(xlUp).Row
    Range("C2").Select
    
    For i = 2 To lastrow
        If ActiveCell.Value > 1 Then
            qty = ActiveCell.Value
            For Count = 1 To qty
                ActiveCell.EntireRow.Select
                Selection.Copy
                ActiveCell.Offset(1, 0).EntireRow.Select
                Selection.Insert Shift:=xlDown
            Next Count
            ActiveCell.Offset(1, 2).Select
        Else
            ActiveCell.Offset(1, 0).Select
        End If
        
    Next i
End Sub



Product codedescriptionQty
GGHUITELEPHONE g5004
YUIONTABLE3
HIOINCHAIR1

<tbody>
</tbody>

So if qty is 4 duplicate row 4 times. If qty is one do no duplicate. Hope this make it clearer. Thanks
 
Upvote 0
thanks a million that's exactly what i needed except with the QTY if it says 7 i only want 7 lines, this leaves the original line and add another seven making it 8 lines in total.
 
Upvote 0
Then just edit this section ....
Code:
For Count = 1 To (qty [B]- 1[/B])

OR this ...

Code:
For Count = 0 To qty

thanks a million that's exactly what i needed except with the QTY if it says 7 i only want 7 lines, this leaves the original line and add another seven making it 8 lines in total.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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