Delete duplicate rows with VBA

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello all

This line of code removes all my duplicate rows based on the primary key in column "A", at the moment it deletes from the last row upwards but I need it to delete from the first row downwards, reason being is that the newest records are always copied after the last row, is this possible please.
Code:
Cells.RemoveDuplicates Columns:=Array(1)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
1. Is the data sorted on column A?

2. About how many rows of data do you have?

3. About what proportion of rows will need to be deleted?

4. Just confirming that you want to only keep the last row of each unique value in column A
 
Upvote 0
Hi Peter

1: At the moment the data is not sorted
2: The rows of data will grow over time, at the moment 300 rows, may well reach about 5,000
3: There always be two duplicates plus some additional new lines - 300 rows means around 130 duplicates with the balance being new lines
4: Yes, the last row for each unique value will always be the latest row that I need to keep
 
Upvote 0
Try this with a copy of your data.
Code:
Sub Del_Dupes()
  Dim a As Variant, b As Variant
  Dim d As Object
  Dim nc As Long, i As Long, k As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  nc = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = UBound(a) To 1 Step -1
    If d.exists(a(i, 1)) Then
      b(i, 1) = 1
      k = k + 1
    Else
      d(a(i, 1)) = 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top