VBA find duplicates and move to another sheet

Darkcloud617

New Member
Joined
Sep 7, 2017
Messages
38
Hello,

We have a code that searches 3 columns for criteria and if a duplicate is found it moves the duplicates to another sheet and deletes the row on the original. The problem is that it does not leave one of the rows on the original sheet. It moves all duplicates to the other sheet, wondering if anyone may know how to alter the code to leave one on the original sheet:

VBA Code:
Sub RemoveDuplicatesCLick()

    Dim lastrow As Long, lastcolumn As Long, rng As Range
  
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
  
    Application.ScreenUpdating = False
  
    'Change sheet where your list is (sheet number must match. You can find sheet number on left of VBA editor)
    With Sheet10
        Set rng = .Range("A1", .Cells(lastrow, lastcolumn))
        .AutoFilterMode = False
      
    'Change wildcard options below (columns chosen for duplicate search): C2&E2&H2
        .Range("A2").Formula = "=C2&E2&H2"
        .Range("A2").AutoFill Destination:=.Range("A2:A" & lastrow)
      
        .Range("B2").Formula = "=COUNTIF($A$2:$A$" & lastrow & ",A2)"
        .Range("B2").AutoFill Destination:=.Range("B2:B" & lastrow)
      
        rng.AutoFilter field:=2, Criteria1:=">1"
      
    'Change "Sheet5" to the sheet you want to export on.
      
        rng.Offset(1, 0).SpecialCells(12).Copy Destination:=Sheet11.Range("A1")
'this is the area that needs to be altered to leave one of the duplicates on the original sheet
        rng.Offset(1, 0).EntireRow.Delete
        Columns("C:w").ClearContents
        .AutoFilterMode = False
      
      
    End With
  
    Set rng = Nothing

    Application.ScreenUpdating = True


End Sub

Thank you in advance for any assistance or thoughts.

Edit: I put a note in the VBA code to show where the area is that needs to leave one of the rows on the original sheet towards the bottom.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Instead of changing the part you commented, what if you changed your formula to show a 1 for the first time something shows up in your data set?
Change this:
VBA Code:
.Range("B2").Formula = "=COUNTIF($A$2:$A$" & lastrow & ",A2)"

To this:
VBA Code:
.Range("B2").Formula = "=COUNTIF($A$2:$A2,A2)"
 
Upvote 0
Solution

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top