+ Reply to Thread
Results 1 to 19 of 19

Customize code for a macro that copies three rows to a new workbook based on a condition

  1. #1
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Customize code for a macro that copies three rows to a new workbook based on a condition

    I have some code here which works great. I would like to customize it in a different way though, and have been unsuccessful at doing so and am therefore reaching out to this community. I will try to explain what I would like to do. What the macro does right now: it copies three rows to a new workbook (Rows 4 and 5 as well as the row of the active cell, based on whether the row of the active cell has non-empty cells). What I would like to do is that I would like the macro to copy the following rows to a new workbook:
    -Row 4 & 5 (like it's doing right now), but based on whether the below row(s) has(have) non-empty cells.
    -any row which i have selected. So instead of just one other row being copied, several other rows should be copied, depending on which rows i select.
    To summarize: Assuming I select row 6,8, & 10. When running the macro, a new workbook is created where the following rows have been copied:
    -Row 4 & 5
    -Row 6,8, & 10 *The condition being however that only those columns are copied where Row 6,8, & 10 are non-empty.
    Assuming I select row 14, 54, 31, 64, 65. When running the macro, a new workbook is created where the following rows have been copied:
    -Row 4 & 5
    -Row 14, 54, 31, 64, 65 *The condition being however that only those columns are copied where Row 14,54,31,64 & 65 are non-empty.
    The overall logic of the current code thus doesn't change. Rather, what changes is that I would like to be able to copy several rows apart from 4&5 instead of just one other (as it is doing now-based on where the active cell is)
    Here is the code, I hope I have been clear. If anyone has any idea where i could start/how i could do this, please let me know, thanks!!

    Please Login or Register  to view this content.
    Links for cross-posts:
    http://stackoverflow.com/questions/1...k-based-on-a-c
    http://www.ozgrid.com/forum/showthread.php?t=173353
    Last edited by philips; 01-03-2013 at 10:18 AM.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    hi

    please take a moment to read the forum rules again paying special attention to #3 and #8 and then amend your post accordingly. thank you ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Hi, thanks for letting me know. How can i delete this thread? I will only keep it on the other forum in that case.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    you don't have to delete it you just need to add links here to your cross-posts in other forums

  5. #5
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    And for my info, how do i delete threads? I've added the links for cross-posts, hope that's fine!

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    you don't. you have a short time to edit them (10 minutes I think) but that's it

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Hi Philips,

    I do not have the time to do it right now, but I will make the changes tomorrow morning, unless somebody else delivers the change quicker
    If you like my contribution click the star icon!

  8. #8
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Thank you so much Olaf. Tomorrow works great for me!

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Ok, this should work

    Please add the Microsoft Scripting Runtime reference to your project to allow for early binding of dictionary objects

    Please Login or Register  to view this content.

  10. #10
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Philips, I have thought of several improvements so version 2 will follow a bit later. I suggest you wait with testing until I have posted the revised code

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Version 2.

    Please remember to add a reference to the Microsoft Scripting Runtime

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Hi Olaf, thank you so much again for taking time to help me. With both versions I get run-time error 13: Type Mismatch and this is the line of code that seems to cause the problem. I have added the microsoft scripting run time reference to the module. I will check online now to see what could cause this.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Philips,

    Strange, the code works fine on some sample data that I have compiled myself. Are you sure about the line number in error? Can you see (by hovering your cursor over the variables) what the values are at the moment of the error?

    or replace the line with

    Please Login or Register  to view this content.
    Not as nice, but maybe less sensitive to problems in case for formula errors and so on
    Last edited by OllieB; 01-04-2013 at 05:29 AM.

  14. #14
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Please Login or Register  to view this content.
    When I replace it with the line of code you just gave me it remains highlighted in yellow with the same values as mentioned here!

  15. #15
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Ok, getting to the cause of the problem

    can you have a look at the contents of row 6 in column HG?

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Philips,

    Never mind, it is probably a formula result error or someting

    Change the line to:

    Please Login or Register  to view this content.
    and it should work fine

  17. #17
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Ah, I understand now!
    So the content of the cell is a vlookup formula that was giving me an N/A error. When the formula works and it is populated with a value rather than n/a, the macro works to perfection. Once again perfect job, and thanks improving my understanding of vba! We're all set! Thank you so much!

  18. #18
    Registered User
    Join Date
    12-21-2012
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Your absolutely right, by replacing, it works also with the error message! It was indeed a formula result error

  19. #19
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Customize code for a macro that copies three rows to a new workbook based on a conditi

    Glad to have been of help. Remember to mark the thread as SOLVED and click the little star if you are happy with my contribution!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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