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
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
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!)
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks