Copy rows in excel multiple "n" times

nefertitinka

New Member
Joined
Sep 14, 2011
Messages
6
Dear all,

I have data in rows, which I have to paste in another files "n" times one after another. "n" is shown in column 1 and equals the required number of copies of the row.

The row data looks like this:
3 company1 share price1
7 company2 share price2
5 company3 share price3
2 company4 share price4
4 company5 share price5

The output should look like this:
3 company1 share price1
3 company1 share price1
3 company1 share price1
7 company2 share price2
7 company2 share price2
7 company2 share price2
7 company2 share price2
7 company2 share price2
7 company2 share price2
7 company2 share price2
5 company3 share price3
5 company3 share price3
5 company3 share price3
5 company3 share price3
5 company3 share price3
2 company4 share price4
2 company4 share price4
4 company5 share price5
4 company5 share price5
4 company5 share price5
4 company5 share price5

Please, help. I have tens of thousands of such inputs and it is not possible to do it manually.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the board

Try the following code - it takes data from the active sheet and duplicates it into a newly created sheet.

Code:
 Sub duplicate()
    Set src = ActiveSheet
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    nr = 1
    Set dst = Sheets.Add
    For i = 1 To lr
        c = Val(src.Cells(i, 1))
        If c = 0 Then c = 1
        src.Rows(i).Copy dst.Rows(nr).Resize(c)
        nr = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Next
End Sub
HTH

PS I hope you're using XL 2007 or later if you have so many inputs - there's no error checking for reaching the last available row, possible when you only have 65,000 available, not likely with over a million. If available space is likely to be an issue, let me know.
 
Last edited:
Upvote 0
Welcome to the board

Try the following code - it takes data from the active sheet and duplicates it into a newly created sheet.

Code:
 Sub duplicate()
    Set src = ActiveSheet
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    nr = 1
    Set dst = Sheets.Add
    For i = 1 To lr
        c = Val(src.Cells(i, 1))
        If c = 0 Then c = 1
        src.Rows(i).Copy dst.Rows(nr).Resize(c)
        nr = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Next
End Sub
HTH

PS I hope you're using XL 2007 or later if you have so many inputs - there's no error checking for reaching the last available row, possible when you only have 65,000 available, not likely with over a million. If available space is likely to be an issue, let me know.

This really works like magic.....Thank you very much for this solution
 
Upvote 0
Hi, I need to copy 47 rows 367 times in Excel 2007. I used your code and modified it but it produces only a new sheet with no data (does the same thing without modifications). Any ideas. Thanks in advance.

2020-05-02 12_30_43-Microsoft Visual Basic - 8 abwkut_order_options.xlsx.jpg
 
Upvote 0
@kavlito.
You should start your own thread and not hijack someone else's thread.
But why would you count the rows in column 47?
I have not checked your code but see what happens if you change the lr line to 1 where it has 47 now.
 
Upvote 0
Excuse me 'Well-known Member'. I was just replying for advice. No need to use 'terroristic' threats.
 
Upvote 0
I guess you got advice. Start your own!

But since you had such a good comeback!
Seriously though, that should not be done. You can always refer to this thread with a hyperlink
Assumes you want to copy/paste up to column 9 (=Column I)
Code:
Sub Try()
    'Thanks to Fluff
    Range("A1:I47").Copy Range("A48").Resize(47 * 367, 9)    '<---- Change the "I" and 9 as required
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,402
Messages
6,124,708
Members
449,182
Latest member
mrlanc20

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