Delete Duplicate Values

jollywood

New Member
Joined
Oct 4, 2011
Messages
40
Hi all, this is my first post here, let's make it a good one!

I have data in a column that I wish to check for duplicates. If I find a duplicate value in that column, I want to delete the entire row and leave only one instance of the datum.

Now, I found the following code and it works just fine...for numbers up to 15 characters:

Sub DeleteDups()

Dim x As Long
Dim LastRow As Long

LastRow = Range("A65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
Range("A" & x).EntireRow.Delete
End If
Next x

End Sub


My problem is that some of my numbers will be up to 20 characters in length. Currently, I use this formula:
=COUNTIF($o:$o,o2&"*")-COUNTIF($o:$o,o2&"?*")
Then, I autofill down an adjacent column and then manually delete the duplicates.


I want the vba code that will properly delete all but one instance and will account for values over 15 digits in length. Thanks in advance!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Use a different test

Code:
For x = LastRow To 1 Step -1
    If Application.Match(Cells(x,1), Columns(1), 0) <> x Then Rows(x).Delete
Next x
 
Upvote 0
When I use that, it deletes nearly everything in the worksheet. Even the ones that aren't duplicates.

**Edit: It works perfectly, thank you! The data I'm testing was in column O, not A, lol. DUH!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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