Macro to delete duplicate data

  • Thread starter Thread starter Tom Harvey
  • Start date Start date
T

Tom Harvey

Hello All,
Can someone tell me how to create a macro to delete duplicate data in a
spreadsheet? Here is what I would like it to do: scan down a column of
data and compare the value in one cell to the cell directly above it, if the
contents of the two cells (say C25 and C26) are the same, the lower row (in
this example row 26) is deleted and cells shifted up. This would continue
until a blank cell is encountered.

Thanks for any suggestions you can give.

Tom
 
Adjust this to your needs, and it should work...

Sub DeleteDuplicates()
Dim c As Long, r As Long, firstRow As Long, lastRow As Long

c = 3 'this is the column to check for duplicates

firstRow = 2 'this assumes you have headers, if you don't change this to 1
lastRow = Cells(Rows.Count, c).End(xlUp).Row

For r = lastRow To firstRow Step -1
If r > 1 Then
If Cells(r, c) = Cells(r - 1, c) Then
Rows(r).Delete
End If
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top