Copy row based on criteria paste row then delete original row and use next row in line

Deinoc

New Member
Joined
Nov 23, 2017
Messages
6
Hello all:
So I have been using this from mrshl9898 that copies a row nth times based on a cell value (A1 to K1, J1 times). It works but I want to use it to loop until the worksheet is empty. I will be processing thousands of rows, so thousand clicks will not work and also "Cells(rownum, 1)" replaces the first cell on sheet2.

Code:
Private Sub CommandButton1_Click()


Dim rownum As Long
Dim copycount As Long
Dim x As Long




copycount = Sheets("Sheet1").Range("J1").Value
Sheets("Sheet1").Range("A1:K1").Copy




x = 0
rownum = 1




Do Until x = copycount




Sheets("Sheet2").Cells(rownum, 1).PasteSpecial xlPasteAll
x = x + 1
rownum = rownum + 1


Loop




End Sub


This is the data example that will be processed thanks!

30Elk (23)|||||Fairgrounds (1)English##################6x
40Elk (1)|Rosemont (4)|Main (15)|Fairgrounds| (3)English##################6x
5Elk (23)|Fairgrounds (1)7x
6Elk (1)||||Rosemont (4)|Main (15)|Fairgrounds (3)3x
7Elk (23)|Fairgrounds (1)2x

<colgroup><col span="11"></colgroup><tbody>
</tbody>
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Not sure what this means:
also "Cells(rownum, 1)" replaces the first cell on sheet2.
<strike></strike>
 
Upvote 0
Try this:
Code:
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Sheet1").Cells(Rows.Count, "J").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = Sheets("Sheet2").Cells(Rows.Count, "J").End(xlUp).Row + 1
    For i = 1 To Lastrow
    ans = Cells(i, "J").Value
    Sheets("Sheet1").Range(Cells(i, "A"), Cells(i, "K")).Copy
    Sheets(2).Range("A" & Lastrowa).Resize(ans).PasteSpecial
    Lastrowa = Sheets("Sheet2").Cells(Rows.Count, "J").End(xlUp).Row + 1
Next
Application.ScreenUpdating = False
End Sub
 
Upvote 0
If you do not need to copy formatting you can try this it would be faster.
It only moves values to Sheet 2

Code:
Private Sub CommandButton3_Click()
'no copy
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Sheet1").Cells(Rows.Count, "J").End(xlUp).Row
Dim Lastrowa As Long
Lastrowa = Sheets("Sheet2").Cells(Rows.Count, "J").End(xlUp).Row + 1
    For i = 1 To Lastrow
    ans = Cells(i, "J").Value
    Sheets(2).Range("A" & Lastrowa & ":K" & Lastrowa).Resize(ans).Value = Sheets("Sheet1").Range(Cells(i, "A"), Cells(i, "K")).Value
    Lastrowa = Sheets("Sheet2").Cells(Rows.Count, "J").End(xlUp).Row + 1
Next
Application.ScreenUpdating = False
 
Upvote 0

Forum statistics

Threads
1,215,577
Messages
6,125,637
Members
449,242
Latest member
Mari_mariou

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