+ Reply to Thread
Results 1 to 16 of 16

Macro to copy rows (in a range) based on a cell criteria

  1. #1
    Forum Contributor
    Join Date
    06-25-2012
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    201

    Macro to copy rows (in a range) based on a cell criteria

    Hi. I'm dealing with a problem. I have a worksheet that has several rows that starts with Back, Lay, Market P/L or Commision in collumn A.
    The thing is i want to copy the rows in a range Ax:Gx (where x is the row number) and paste then (AS VALUES) in a new sheet (named DATA) in cell A1. But i want it to copy only the rows in that range where the cell in the collumn A starts as Back. If it says Lay, or Commision or Market P/L, that row in that range wont be copied.
    The result would be a DATA sheet where from A1 to Ax with no gaps between all rows only be visible the rows which start as Back in A cell.
    I attached a file with the data so its more perceptible,
    If anyone could help i would be really apreciated.

    Thank you.
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to copy rows (in a range) based on a cell criteria

    Hi Taislin

    Sounds like an AutoFilter issue.

    First put a Header Row in Row 1 by inserting a new Row, place a value in Cell A1 (any value, matters not...make it simply X). Then Filter on Column A, Copy the Filtered Data to the Sheet called Data, Paste Special Values. You can do all this with the Macro Recorder then clean up the Code.

    Try this then come back with issues and we'll be glad to help you through it.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

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

    Re: Macro to copy rows (in a range) based on a cell criteria

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    06-25-2012
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Macro to copy rows (in a range) based on a cell criteria

    @jaslake

    Thank you for your idea. Didn't have though about the filters.... worked great and recorded the code and cleaned. The thing is when i copy the range may vary and i cant record this so have to change the code according to this sittuation and dont really know how to do that. But was a very good idea. Thank you.

    @AB33

    Your code is just so gracefull that works like a charm. Thank you.

    For jaslake thank you for the idea but i think the AB33 is just what i need without much work.
    Thank you both for the help.

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Macro to copy rows (in a range) based on a cell criteria

    Another way

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

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

    Re: Macro to copy rows (in a range) based on a cell criteria

    Taislin,
    You are welcome!

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to copy rows (in a range) based on a cell criteria

    Hi Taislin

    If you post your Code (which you developed, by the way), I'll show you how to deal with this
    the range may vary
    I have no issue with AB33's code other than it loops through all the Rows...you don't really need to do that.

    You decide...

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

    Re: Macro to copy rows (in a range) based on a cell criteria

    John,
    You are right!
    No need to loop through, but as I said on many occasions in this forum, I need to overcome my phobia for "Filter" and "Sorting".
    Hopefully soon!

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to copy rows (in a range) based on a cell criteria

    @AB33

    The important issue is that Taislin has a resolution...we can write Code all day to address the issue with multiple approaches. My first thought in looking at the File was AutoFilter. The whole process, regardless if selecting Back, Lay, Market P/L or Commision, can be automated.

    It really depends on what Taislin wants/needs.

  10. #10
    Forum Contributor
    Join Date
    06-25-2012
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Macro to copy rows (in a range) based on a cell criteria

    Thank you again for all responses. Dindt notice all fuzz around this after my last post. Just to say that i went back to jaslake option through autofilter, because the macro from AB33 was getting slow (maybe because the amount of data).
    The autofilter option, i just made a macro that selects all data then filters the BACKS and copy then and paste as values in the other sheet and works really well.
    The macro provided by mike7952 didnt worked. So i went for this solution.

    Thank you all for the quick responses.

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Macro to copy rows (in a range) based on a cell criteria

    The macro provided by mike7952 didnt worked
    Just curious what do you mean didn't work? Does it not work on your example workbook? What error are you getting?

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to copy rows (in a range) based on a cell criteria

    Hi Taislin

    Would you like the option to select Back or Lay or Market P/L or Commision?

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

    Re: Macro to copy rows (in a range) based on a cell criteria

    Taislin,
    Part of the reason the code getting slow was that it copied an entire row, instead of specified range.
    Change this line and should be able to see the difference in speed.

    Please Login or Register  to view this content.
    INTO

    Please Login or Register  to view this content.

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

    Re: Macro to copy rows (in a range) based on a cell criteria

    Mike,
    Your code works too, but your loop missed the first row. I.e, Loop starts at 1 with the attached.

  15. #15
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Macro to copy rows (in a range) based on a cell criteria

    Ooo I missed that there was no headers. But I am still wondering what OP means by doesn't work?

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

    Re: Macro to copy rows (in a range) based on a cell criteria

    Hmmmm!
    The only reason I can guess is that your output is in new sheet while the OP had expected in sheet called "DATA". This is just my poor guess.

+ 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