I have attempted to create a macro which scan cells from column D to find duplicates and move the row where the 'second' duplicate is found (i.e. the one with the highest row number) to first empty row, i.e. right at the bottom of the list.
As an example, the worksheet has no more than 100 rows, the macro starts to look for the value in D1 and compare it to D2, if the values are the same, then row 2 is moved down to row 100. So on so forth for the remaining cells in column D until a blank row is met, then the macro simply deletes all empty row.
Here is a sample of the code I have been using:
Sub Move_Duplicates()
Dim ws1 As Worksheet: Set ws1 = ActiveSheet
Dim iListCount As Integer
Dim iCtr As Integer
Dim r As Long
' Turn off screen updating to speed up macro.
Application.ScreenUpdating = True
' Get count of records to search through.
iListCount = Range("B65536").End(xlUp).Row
ws1.Range("D1").Select
' Loop until end of records.
Do Until ActiveCell = ""
' Loop through records.
For iCtr = 1 To iListCount
' Don't compare against yourself.
' To specify a different column, change 1 to the column number.
If ActiveCell.Row <> ws1.Cells(iCtr, 4).Row Then
' Do comparison of next record.
If ActiveCell.Value = ws1.Cells(iCtr, 4).Value Then
' If match is true then delete cut row and paste further down 100 rows
Rows(iCtr).Cut
Rows(Offsetcount + 100).Insert Shift:=xlDown
' Increment counter to account for deleted row.
iCtr = iCtr + 1
End If
End If
Next iCtr
' Go to next record.
ActiveCell.Offset(1, 0).Select
Loop
For r = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1
If Cells(r, 2) = "" Then Rows(r).Delete
Next r
'Application.ScreenUpdating = True
End Sub
The macro runs however it does not provide a consistent outcome according to the source data I use. For example the macro moves rows to the bottom of even if there are no duplicates.
Any help would be appreciated.
Thanks,
Antoine
Bookmarks