ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to delete duplicate data (https://www.excelbanter.com/excel-programming/302566-macro-delete-duplicate-data.html)

Tom Harvey

Macro to delete duplicate data
 
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



TommyGun

Macro to delete duplicate data
 
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

Tom Harvey

Macro to delete duplicate data
 
Thanks for the quick reply.
I will give it a try.

Tom
"TommyGun" wrote in message
...
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





All times are GMT +1. The time now is 08:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com