+ Reply to Thread
Results 1 to 18 of 18

Copy rows x number of times into a new sheet

  1. #1
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Copy rows x number of times into a new sheet

    I have an inventory worksheet that has details about different products, such as product code, description, price, quantity.
    I need to copy these cells into a new spreadsheet such that each row is copied x number of times - based on the number in the quantity column.
    I need this in order to import this new sheet into a word mail merge for the purpose of printing some barcodes.

    i.e.

    Product Description Price Quantity
    0001 Table $50 2
    0002 Vase $20 2
    0003 Painting $60 1
    0004 Tray $30 3

    I need to copy this data into a new spreadsheet such that:

    Product Description Price Quantity
    0001 Table $50 2
    0001 Table $50 2
    0002 Vase $20 4
    0002 Vase $20 4
    0002 Vase $20 4
    0002 Vase $20 4
    0002 Vase $20 4
    0003 Painting $60 1
    0004 Tray $30 3
    0004 Tray $30 3
    0004 Tray $30 3


    Please assist me with with an appropriate macro that will enable me to achieve this.
    Thank you.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    How did you get 6 Vase while the quantity shows 2?

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    Assuming it was 2 vase.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy rows x number of times into a new sheet

    Maybe:

    Please Login or Register  to view this content.
    I used Sheet2 as the new sheet. If you want the macro to create a new sheet the let me know.

  5. #5
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    Sorry vase quantity was meant to be 5.

    The macro is only copying the column titles, i.e row 1.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    No, it copies all rows. Please look at the attached sheet2.

  7. #7
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    Thank you for your quick reply. I tried to copy the macro sub into my spreadsheet and it was only copying the column titles. I will move my data to your spreadsheet instead. Thanks again.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    Is the code now working or not? If not, please attach the sample you are working on.

  9. #9
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    It working.

    What change do I need to make if I wish to have 6 columns copying instead of just 4?

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    Expand the array line to 6

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    That's giving me a "runtime error '9': script out of range.

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    Oops!

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    I'm still getting the same error.

    The quantity is still in column D. I have just added information in E & F.

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    Please show me where you are getting an error.

  15. #15
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    Here's a print screen...
    Attached Images Attached Images

  16. #16
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    Ok, I decided to move quantity to the final column & changed the macro as follows:

    Sub Repeat_x()

    Dim x, i As Long, NR As Long, LR&

    With Sheets("Sheet1")
    LR = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
    x = .Range("A1:F" & LR)
    End With
    NR = 2
    Application.ScreenUpdating = False
    With Sheets("Sheet2")
    For i = 2 To UBound(x, 1)
    If (x(i, 6)) > 0 Or IsError(x(i, 6)) Then
    .Cells(NR, "A").Resize(CLng(x(i, 6)), 6) = Array(x(i, 1), x(i, 2), x(i, 3), x(i, 4), x(i, 5), x(I, 6))
    NR = NR + x(i, 6)
    End If
    Next i
    Application.ScreenUpdating = True
    End With
    End Sub


    This appears to be working.

    Thank you so much for your assistance. God bless you

  17. #17
    Registered User
    Join Date
    09-16-2013
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Copy rows x number of times into a new sheet

    This macro was working perfect, with no issues. However, today, suddenly the macro has stopped running. I am not getting any errors, just the macro is not running. Can someone please help me? I've tried to look through the spreadsheet, but cannot find a reason for this.

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Copy rows x number of times into a new sheet

    Jeni,
    Please use code tags with your code as per forum's rule.
    The issue can easily be solved if you could attach the code with the sample you are working on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy and insert rows n number of times + operations on data
    By rogerissimo in forum Excel General
    Replies: 2
    Last Post: 10-08-2010, 12:47 PM
  2. Copy and insert rows n number of times + operations on data
    By rogerissimo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2010, 11:35 AM
  3. Loop/copy rows variable times to new sheet
    By ianmaggy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2009, 07:18 AM
  4. I need a macro to copy each of 2000 rows 12 times in same sheet
    By Cyn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2006, 04:30 PM
  5. [SOLVED] Loop/copy rows variable times to new sheet
    By Patti in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-01-2005, 05:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1