Hi Amlan
This code is in the attached. It first makes a copy of the original data to Sheet2. Then it creates 3 new column fields, 1 row for each record. I'm using Columns T, U and V for this. If this needs to be moved further to the right let me know. I'm NOT using your Column N.
In Column T I'm placing the String Length of Column E.
In Column U I'm placing the original sort sequence (the Row Number if you will)
In Column V I'm placing a concatenation of Columns A, B, C, D and F.
The only way I could accommodate this
i would wish to have another macro to show me the original result on the same active sheet so that i can compare and see whether results are correct
was to make a copy of the original data on Sheet2.
There are notes in the code that explain what it's doing. Let me know of issues.
Option Explicit
Sub Delete_Dups()
Dim LR As Long
Dim rng As Range
Dim cel As Range
Dim i As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
.Cells.Copy Destination:=Sheets("Sheet2").Range("A1") 'make copy of original data
LR = .Cells.Find("*", .Cells(Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
.Range("U2").Value = 2 'assign row numbers
.Range("U3").Value = 3 'assign row numbers
.Range("U2:U3").AutoFill Destination:=.Range("U2:U" & LR), Type:=xlFillDefault 'fill row numbers to all records
.Range("V2").Formula = "=CONCATENATE(A2,B2,C2,D2,F2)" 'put A,B,C,D & F all in one cell so we can compare
.Range("V2").AutoFill Destination:=.Range("V2:V" & LR), Type:=xlFillDefault 'fill it down to all records
.Range("T2").Formula = "=LEN(E2)" 'get the length of E text string
.Range("T2").AutoFill Destination:=.Range("T2:T" & LR), Type:=xlFillDefault 'fill it down to all records
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("V2:V" & LR), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'sort first by the concatenated string
.Sort.SortFields.Add Key:=Range("T2:T" & LR), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'then sort by E string length
With Sheets("Sheet1").Sort
.SetRange Range("C2:V" & LR)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set rng = Range("V2:V" & LR) 'look at each string in column V...the concatenated string
With rng
For i = LR To 1 Step -1 'start at the bottom of the file and iterate upwards
If rng(i).Value = rng(i).Offset(-1, 0).Value Then 'if they match then
If Range("T" & rng(i).Row).Value >= Range("T" & rng(i).Row).Offset(-1, 0).Value Then 'check the E string length
Range("T" & rng(i).Offset(-1, 0).Row).EntireRow.Delete 'if it fails muster delete the matching record
Else
Range("T" & rng(i).Row).EntireRow.Delete 'or delete this record
End If
End If
Next i
End With
LR = .Cells.Find("*", .Cells(Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("U2:U" & LR), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 'sort the file back to it's original order
With Sheets("Sheet1").Sort
.SetRange Range("A1:V" & LR)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
.Columns("T:V").ClearContents 'get rid of the stuff we added
End With
Application.ScreenUpdating = True
End Sub
Bookmarks