View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
[email protected] meh2030@gmail.com is offline
external usenet poster
 
Posts: 135
Default VBA for deleting duplicate rows

On Jun 26, 11:06 am, ulfb wrote:
Hi
Amateur needs some help please: Sheet40, col B contains item id:s - I need
to keep lowest numbers and delete all duplicate rows, which can be one or
more. Should be controlled by code - not a manually started macro. Understand
I should start with sorting - but then?
/ulf


This assumes that you are deleting duplicates in Col B on Sheet40.

After you sort the data by Col B you can do something like the
following (This is not tested):

Sub delDuplicates()

Dim counter As Integer
Dim a As Integer
Dim currCell As Variant
Dim nextCell As Variant
Dim delRng As Range

counter = Range("b1").CurrentRegion.Rows.Count

For a = counter To 2 Step -1
currCell = Range("b" & a).Value
nextCell = Range("b" & a).Offset(-1, 0).Value

Set delRng = Range("b" & a).Offset(-1, 0)

If currCell = nextCell Then
'Range("b" & a).Offset(-1, 0).EntireRow.Delete
delRng.EntireRow.Delete
End If
Next

End Sub