+ Reply to Thread
Results 1 to 10 of 10

Copy and paste row based on cell value

Hybrid View

  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:
    Sub filtr()
        Range("AA2").Select
        ActiveCell.Value = "=ISNUMBER(MATCH(Sheet2!$Z1,'[income nums.xlsx]Sheet1'!$A$1:$A$18,0))"
        
        Selection.AutoFill Destination:=Range("AA2:AA500"), Type:=xlFillDefault
        '500 is the most i biggest set i predict it to be
        Range("AA2:AA500").Select
        Range("AA1").Select
        
        Selection.AutoFilter
      
        ActiveSheet.Range("$A$1:$AA$500").AutoFilter Field:=27, Criteria1:="TRUE"
     'if there are none that are true, msgbox = "nothing to filter" and removes any changes made to sheet
     
     'i want to cut all rows that are TRUE here. not sure how to have the macro pick these up and cut
        Range("A31:Z31").Select
        Selection.Cut
        
    'want to make a new sheet called "Removed" that pastes these values row after row
        Sheets("Sheet2").Select
        ActiveSheet.Paste
            
        'turn off filter
        Range("AA1").Select
        Selection.AutoFilter
        Columns("AA:AA").Select
        Selection.Delete Shift:=xlToLeft
        Selection.xlToLeft
    End Sub

  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".

    Code removed...see below
    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
    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.

    Option Explicit
    
    Sub MyFilter()
    Dim LR As Long, BR As Long, Matches As Boolean
    
    With Sheets("Sheet1")
    'find range of data on sheet
        LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), _
                SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    'add key column
        .Range("AA1") = "Key"
        .Range("AA2:AA" & LR).FormulaR1C1 = _
            "=ISNUMBER(MATCH(Sheet2!R1C26,'[income nums.xlsx]Sheet1'!R1C1:R18C1,0))"
    'filter for matches
        .Range("AA:AA").AutoFilter
        .Range("AA:AA").AutoFilter Field:=1, Criteria1:="TRUE"
    'cut matches to another sheet
        BR = .Range("AA" & .Rows.Count).End(xlUp).Row
        If BR > 1 Then
            With .Range("AA2:AA" & BR).SpecialCells(xlVisible)
                .EntireRow.Copy _
                    Sheets("Removed").Range("A" & Rows.Count).End(xlUp).Offset(1)
                .EntireRow.Delete xlShiftUp
            End With
            Matches = True
        End If
        
    'turn off filter
        .AutoFilterMode = False
        .Range("AA:AA").ClearContents
    End With
    
    If Matches Then MsgBox "Matches moved" Else MsgBox "No matches"
        
    End Sub

  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

    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?

  10. #10
    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

+ 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