Macro to delete duplicate data

T

Tony

I need a macro to delete duplicate data. I would like to
capture the data in Range ("B1") and check it against each
value in column B. If a duplicate is found, I would like
to delete the first instance. It is possible that the
data is duplicated 2 or more times.

For example:

A B
1 123 ABC
2 234 CDE
3 123 ABC
4 567 FGH

In this case, I would like row 1 to be deleted. Then I
would like the next value ("B2" = CDE above) to be
captured and compared to the rest of the data. It is
possible that I have a range of 10, 20, 30 or more
values. The number of rows changes frequently. Any help
would greatly be appreciated.

Tony
 
M

mudraker

Sub DelDuplicate()
Dim Comp
Dim iRow As Integer
Dim i As Integer
Do
iRow = iRow + 1
Comp = Range("b" & iRow)
If Comp = "" Then
Exit Do
End If

For i = iRow + 1 To Cells(Rows.Count, "B").End(xlUp).Row
If Range("b" & i) = Comp Then
Range("b" & iRow).Delete Shift:=xlUp ' delete cell
or
Rows(iRow).Delete Shift:=xlUp ' delete row
iRow = iRow - 1
Exit For
End If
Next i
Loop
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

Similar Threads


Top