remove duplicate if cell appears twice macro

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
782
Office Version
  1. 365
  2. 2010
the removed duplicates function because other columns are different

let's assume the duplicated value appears in column A

i'd like the macro to delete only ONE of the duplicates

...

i have a helper column in Y that does a countif and this code


Code:
    finalrow = Cells(Rows.Count, "A").End(xlUp).Row


For i = finalrow To 2 Step -1

If Range("y" & i) > 1 Then
Range("y" & i).EntireRow.Delete
Exit Sub
End If
Next i

but the issue with this code is that it would stop once it finds the any row with 2 and there could be multiple rows with duplicate values in column A (this code only works if there's only two rows of data with duplicates, but, not, say, 4)
 
OK, so the duplicate range is column D, so if you change the countif range to D:D it will work for the whole set of data
VBA Code:
For i = finalrow To 2 Step -1
If application.worksheetfunction.countif(Range("D:D"), Range("D" & i) > 1 Then
    Range("D" & i).EntireRow.Delete
End If
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If the column that has the duplicates is in column D then the code would be:

VBA Code:
For i = finalrow To 2 Step -1
If application.worksheetfunction.countif(Range("D:D"), Range("D" & i) > 1 Then
    Range("D" & i).EntireRow.Delete
End If
If you want it to be versatile you can change the column as a variable like this:
VBA Code:
DC = "D"
For i = finalrow To 2 Step -1
If application.worksheetfunction.countif(Range(DC & ":" & DC), Range(DC & i) > 1 Then
    Range(DC & i).EntireRow.Delete
End If
Then just change the DC variable depending on the column you're checking
 
Upvote 0
If the column that has the duplicates is in column D then the code would be:

VBA Code:
For i = finalrow To 2 Step -1
If application.worksheetfunction.countif(Range("D:D"), Range("D" & i) > 1 Then
    Range("D" & i).EntireRow.Delete
End If
If you want it to be versatile you can change the column as a variable like this:
VBA Code:
DC = "D"
For i = finalrow To 2 Step -1
If application.worksheetfunction.countif(Range(DC & ":" & DC), Range(DC & i) > 1 Then
    Range(DC & i).EntireRow.Delete
End If
Then just change the DC variable depending on the column you're checking

tks
 
Upvote 1
@daveyc18 I have unchecked post#12 as the solution simply because it cannot work.
You're right, I missed a bracket. here is the working code
VBA Code:
Finalrow = Cells(Rows.Count, "A").End(xlUp).Row
DC = "D"
For i = Finalrow To 2 Step -1
    If Application.WorksheetFunction.CountIf(Range(DC & ":" & DC), Range(DC & i)) > 1 Then
        Range(DC & i).EntireRow.Delete
    End If
Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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