+ Reply to Thread
Results 1 to 10 of 10

Copy and paste row based on cell value

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Copy and paste row based on cell value

    Hi all,

    I have a workbook with a worksheet ("work1") with names in Column A and serial numbers in Column B.

    In this workbook is a second worksheet ("work2") with another list of serial numbers.

    I need the macro to look for serial numbers in work1 (column B) from work2.

    If cell is found, cut row out of work1 and paste in new worksheet.

    Thanks!
    Last edited by contra76; 10-18-2010 at 10:03 PM. Reason: clarity

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and paste row based on cell value

    1) On sheet1 make sure row1 is a "title row", not part of the data.

    2) In empty column C, put this formula in C2 and copy down:

    =ISNUMBER(MATCH($B2, 'Spreadsheet 2'!$A;$A, 0))

    A series of True/False answers will appear.

    3) Click the top of the new column and select Data > Filter > AutoFilter

    4) Filter the new column by TRUE

    5) Highlight all the visible data rows from A:B and cut them, paste to another sheet

    6) Turn off the AutoFilter and clear the new column.


    You can actually record those steps into a macro pretty easily.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copy and paste row based on cell value

    Thanks JBeaucaire; your thorough responses are much appreciated.

    I understand the mechanics of putting one together as well as the recording function etc, but I was hoping for a whole macro because I noticed some errors that I get stuck on (I'm on a deadline) which I end up trying to band-aid inefficiently, example:
    If I wrote a command to filter and cut, I know I'll screw up and get a debug error when there's nothing to filter out, and things of that nature.

    If it's not too much can you please package this into a good to go macro? I have to modify it to fit within save functions and directories anyway so I know how to read/amend I just know people on this forum tend to code so much quicker and efficiently than I can.

    Thanks again.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and paste row based on cell value

    From the steps already provided, let's see you at least use them in your own workbook to do the filtering once and cut/paste to the desired destination. Let it record you doing it twice.

    Post that macro and I'll add some error checking for you.

  5. #5
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copy and paste row based on cell value

    Thanks for the assistance. My poorly written code below:
    Please Login or Register  to view this content.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and paste row based on cell value

    See, it's so much easier to code when we can see what you did. Make sure you already have sheets called "Sheet1", "Sheet2", and "Removed".

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 10-20-2010 at 03:23 AM.

  7. #7
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copy and paste row based on cell value

    Wow. Impressed to say the least.

    How does LR function find the number of rows exactly? i've never seen this code before. i have like 20 columns, many with blank cells (including the one that we're referencing), so I fear it will miss some. One column (column J) will always have the full cells/true number of rows though.

    Feeling happy about this

    I also notice that you don't use the LR function again after you define it in the beginning. right?
    Last edited by contra76; 10-19-2010 at 08:07 PM.

  8. #8
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copy and paste row based on cell value

    Arghhh.

    Ran the macro and it ends up that when the filter function is ON, and rows are cut, it ends up cutting a bunch of the "hidden" rows in between (because the filter is hiding them).

    Work around?

  9. #9
    Registered User
    Join Date
    09-23-2010
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copy and paste row based on cell value

    delete this post plz
    Last edited by contra76; 10-19-2010 at 08:07 PM. Reason: delete

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and paste row based on cell value

    Quote Originally Posted by contra76 View Post
    How does LR function find the number of rows exactly? i've never seen this code before. i have like 20 columns, many with blank cells (including the one that we're referencing), so I fear it will miss some. One column (column J) will always have the full cells/true number of rows though.
    The LR variable is searching from all the cells from the bottom of the worksheet upward until it finds a cell with anything in it, then storing that row in the LR.


    I also notice that you don't use the LR function again after you define it in the beginning. right?
    The LR was supposed to appear in a couple of lines of code after that, but missed the first posting, don't know how I managed that. Here's the corrected code along with a different copy/paste/delete from old sheet method that shouldn't include any of the hidden rows.

    Please Login or Register  to view this content.

+ 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