+ Reply to Thread
Results 1 to 3 of 3

Search column a for duplicate data and copy entire row to duplicate sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    11-16-2011
    Location
    Logan, WV
    MS-Off Ver
    Excel 2010
    Posts
    25

    Search column a for duplicate data and copy entire row to duplicate sheet

    I need a macro to search column A on sheet one for duplicates and each row that is a duplicate copy entire row to a sheet named duplicates. Can anyone help?



    Thanks,
    Allen

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search column a for duplicate data and copy entire row to duplicate sheet

    Hello Allen,

    This macro assumes the worksheets are named "Sheet1" and "Duplicates". Row one on both worksheets is also assumed to to have headers. Copy and paste this code into a VBA module. You can then run the macro manually or attach it to a button.
    
    Sub CopyDuplicates()
    
        Dim Cell As Range
        Dim DstWks As Worksheet
        Dim Key As String
        Dim Matches As Collection
        Dim R As Long
        Dim Rng As Range
        Dim RngEnd As Range
        Dim SrcWks As Worksheet
        
            Set SrcWks = Worksheets("Sheet1")
            Set DstWks = Worksheets("Duplicates")
            
                Set Rng = SrcWks.Range("A2")
                Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
                If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = SrcWks.Range(Rng, RngEnd)
            
                DstWks.UsedRange.Offset(1, 0).Clear
                R = DstWks.UsedRange.Row + 1
                
                Set Matches = New Collection
                
                    For Each Cell In Rng
                        Key = Trim(Cell)
                        On Error Resume Next
                            Matches.Add Cell.Row, Key
                            If Err <> 0 Then
                                Cell.EntireRow.Copy DstWks.Rows(R)
                                R = R + 1
                            End If
                        On Error GoTo 0
                    Next Cell
                    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845

    Re: Search column a for duplicate data and copy entire row to duplicate sheet

    Hi,

    Here's some code that may help. It looks at column A then move the dups to the Dup sheet.
    Sub DupMove_code()
    Dim I As Long
    Dim lastrow
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    For I = 1 To lastrow
        If WorksheetFunction.CountIf(Range("A1:A" & I), Range("A" & I)) > 1 Then
            Rows(I).Copy Destination:=Sheets("Duplicate").Range("A" & Sheets("Duplicate").Range("A65536").End(xlUp).Row + 1)
        End If
    Next
    End Sub
    Last edited by Charles; 03-20-2012 at 08:23 PM. Reason: Ops Leith posted but here's my version
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

+ 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