Formula to Delete Both Duplicates.

savbci

New Member
Joined
Apr 15, 2003
Messages
16
I have a row of numbers sorted. I need to run a macro to detect the duplicates and automatically delete both of them. Can i do this???
Example in column A:
4
4
5
6
7

Want it to look like this:
5
6
7
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
One way:

Code:
Sub Test()
    Dim Rng As Range
    Columns("A:A").Insert Shift:=xlToRight
    Set Rng = Range("A1:A" & Range("B65536").End(xlUp).Row)
    With Rng
        .FormulaR1C1 = "=COUNTIF(R1C2:R5C2,RC[1])"
        .Resize(, 2).AutoFilter Field:=1, Criteria1:=">1", Operator:=xlAnd
        .SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With
    Columns("A:A").Delete Shift:=xlToLeft
End Sub
 
Upvote 0
WELCOME TO THE BOARD!

Here's another method:

Code:
Sub Delete_Duplicates()
Dim r As Long
Dim r2 As Long
Dim CurrentVal As String
Dim Unique As Boolean
    For r = 1 To Range("A65536").End(xlUp).Row
        CurrentVal = Range("A" & r).Value
        Unique = True
        For r2 = r + 1 To Range("A65536").End(xlUp).Row
            If Range("A" & r2).Value = CurrentVal Then
                Unique = False
                Range("A" & r2).EntireRow.Delete
                r2 = r2 - 1
            End If
        Next r2
        If Unique = False Then
            Range("A" & r).EntireRow.Delete
            r = r - 1
        End If
    Next r
End Sub
 
Upvote 0
Re: WELCOME TO THE BOARD!

Here's another method:

Code:
Sub Delete_Duplicates()
Dim r As Long
Dim r2 As Long
Dim CurrentVal As String
Dim Unique As Boolean
    For r = 1 To Range("A65536").End(xlUp).Row
        CurrentVal = Range("A" & r).Value
        Unique = True
        For r2 = r + 1 To Range("A65536").End(xlUp).Row
            If Range("A" & r2).Value = CurrentVal Then
                Unique = False
                Range("A" & r2).EntireRow.Delete
                r2 = r2 - 1
            End If
        Next r2
        If Unique = False Then
            Range("A" & r).EntireRow.Delete
            r = r - 1
        End If
    Next r
End Sub


hello, i have tried the above code but it seems to loop for a long time and finally crash due to he fact that -(i think is trying to delete empty cells that the code recognize them as duplicates) . I don't know if my guess is correct or not but any how the code is failing for me. Is there any way to change the above code to find duplicates that have a value or for example are greater than zero??
My problem is actually very similar previous guy.
I have a several columns and rows with data where i want to automatically find the duplicate values that exist in the entire column A and delete both duplicate values with their rows!

PS: I don't want to use any manual drag-down, i just want to be an automatic function which deletes BOTH dublicates if they exist!
thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,589
Messages
6,179,744
Members
452,940
Latest member
rootytrip

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