+ Reply to Thread
Results 1 to 18 of 18

VBA code to Copy entire row to new sheet multiple times

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Smile VBA code to Copy entire row to new sheet multiple times

    Hi

    Im new here and to VBA and would appreciate some help. I have a set of data, and one of the columns named 'Steps', has single digit values eg 1,2,4,5 etc

    I would like to copy each entire row to a new sheet. The number of times i would like to copy a given row will be whatever the number is in the steps column.

    The size of the data set changes, so will need code to find the last row and then loop through the whole dataset to do the copying.

    Your help is appreciated!

    Cheer

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

    Re: VBA code to Copy entire row to new sheet multiple times

    Doable! Please attach a sample. Go to advance, then attachment.

  3. #3
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Wink Re: VBA code to Copy entire row to new sheet multiple times

    Hi

    Thanks for your response. This is a short example file attached.

    cheers
    Attached Files Attached Files

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

    Re: VBA code to Copy entire row to new sheet multiple times

    The result is on sheet2
    Attached Files Attached Files
    Last edited by AB33; 05-10-2013 at 06:40 AM.

  5. #5
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    Thanks. im assuming you renamed sheet1 "raw data" in your testing?

    cheers

  6. #6
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    and apologies for the following newbie question.. why am i getting an "application defined or object defined error" when i try to run it?

    cheers

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

    Re: VBA code to Copy entire row to new sheet multiple times

    Sorry! My bad!
    I attached the wrong code and sheet name. Please see corrected attached post #4.

  8. #8
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    Thanks for your work on this.

    however, sorry but im still getting an "application defined or object defined error" when i F8 through it, on the first line after "for i = 2 to LR".
    Could it be my machine? its windows 7 with excel 2007

    cheers

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

    Re: VBA code to Copy entire row to new sheet multiple times

    I re-run the code and did not get an error, so it is most likely that the error is on your data.

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

    Re: VBA code to Copy entire row to new sheet multiple times

    Did you get the error on the attached, or your actual sheet data?
    Could it be my machine? its windows 7 with excel 2007
    No, I do not think so. The error is on the code and we need to find it.

  11. #11
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    dies "Dim i&" mean Dim i as integer?

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

    Re: VBA code to Copy entire row to new sheet multiple times

    You still have not answered my question and seem we are going around the circle. Please attach the sample with a code which gives you the error.

  13. #13
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    sorry, ok ive attached another workbook which shows a subset of whole data (taken some columns out for data security reasons)
    Basically im trying to adapt your code to work with this data. The steps column in my data is column 27 -(AA)


    The macro should go through each line (over 1000 at present) and read the value of the 'steps' column and copy that current row
    by the number of times stated on the steps column. your macro does that when i click the 'run me' button you included.

    But when i F8 through it for some strange reason it does not and im struggling to adapt the code to read the value in column 27.

    please assist and thanks

    PS: its the code in the For loop that im struggling to get to work.
    Attached Files Attached Files
    Last edited by wazimu13; 05-10-2013 at 10:26 AM.

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

    Re: VBA code to Copy entire row to new sheet multiple times

    Corrected.
    Attached Files Attached Files
    Last edited by AB33; 05-11-2013 at 05:44 AM.

  15. #15
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    hi, thanks for correcting but i cant see any code or modules in the vba editor? do i need to unhide anything?
    cheers

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

    Re: VBA code to Copy entire row to new sheet multiple times

    Ooops,
    You are right!
    When I checked the attached, it appears to be macro saved, but there is no code on it. I have now double checked and has a code. It was one of those days.

  17. #17
    Registered User
    Join Date
    04-19-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: VBA code to Copy entire row to new sheet multiple times

    Hi, thanks no problem. I have managed to modify it a little after you did the heavy lifting and it works. Basically, whenever it encountered a zero in the steps column, it crashed. I didnt want to bother you with a request to check for zeros (though u can show me code for it if its not too much trouble )
    so i cheated by deleting all rows where a zero appeared.

    Your help is very much appreciated! Cant thank u enough! Have a virtual pint on me. God bless.

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

    Re: VBA code to Copy entire row to new sheet multiple times

    Okay! I normally incorporate in my code for that purpose, but your were not luckily from the word go. The code was riddled with the errors.
    I have now two codes for you(On the same module). The first one is the easiest way to cheat the error by including an error trap line, but it is not recommended.

    The second code does not copy a row if the step column is less than 0.

    Hopefully one of them works for you.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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