VBA for deleting duplicate rows

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Keep the lowest number, based on the value in which column?
Should be controlled by code - not a manually started macro.

What do you mean by that?

Bernie
MS Excel MVP
 
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
 
Sorry - let me try to explain more carefully_

- I need to keep rows with lowest row number after sorting on B and M
(sometimes another cols) and delete possible other rows with same id. Deleted
rows should not leave empty rows.

- processing is automated - sub should be called from other sub with no user
involvment

thank you!
/ulf
 
/ulf,

Sub KeepLowestRow()
Dim myRow As Long

myRow = Cells(Rows.Count, 2).End(xlUp).Row
Range("C1").EntireColumn.Insert Shift:=xlToRight
Range("C1:C" & myRow).FormulaR1C1 = "=COUNTIF(R1C2:RC[-1],RC[-1])"
Range("C1:C" & myRow).AutoFilter Field:=1, Criteria1:="<>1"
Range("C2:C" & myRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Range("C2:C" & myRow).AutoFilter
Range("C2").EntireColumn.Delete
End Sub

HTH,
Bernie
MS Excel MVP
 
Beautiful! Simple, fast, easy to adapt..
Thank you Bernie!
/ulf


Bernie Deitrick said:
/ulf,

Sub KeepLowestRow()
Dim myRow As Long

myRow = Cells(Rows.Count, 2).End(xlUp).Row
Range("C1").EntireColumn.Insert Shift:=xlToRight
Range("C1:C" & myRow).FormulaR1C1 = "=COUNTIF(R1C2:RC[-1],RC[-1])"
Range("C1:C" & myRow).AutoFilter Field:=1, Criteria1:="<>1"
Range("C2:C" & myRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Range("C2:C" & myRow).AutoFilter
Range("C2").EntireColumn.Delete
End Sub

HTH,
Bernie
MS Excel MVP


ulfb said:
Sorry - let me try to explain more carefully_

- I need to keep rows with lowest row number after sorting on B and M
(sometimes another cols) and delete possible other rows with same id. Deleted
rows should not leave empty rows.

- processing is automated - sub should be called from other sub with no user
involvment

thank you!
/ulf
 

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