remove duplicate if cell appears twice macro

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
755
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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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