Sub Find_RemoveDuplicatesWithinRange()
'*************************************************************************************************** '* Macro recorded by Yogesh Gupta, smiley123z@gmail.com, Ygblogs.blogspot.com on September 13, 2009 * '*************************************************************************************************** Dim cRow As Long Dim sRow As Long Dim lRow As Long Dim sCol As Long Dim lCol As Long Dim dColumn As Long Dim D As Long Dim sCell As Range Dim dRange As Range
On Error Resume Next Set dRange = Application.InputBox(Prompt:= _ "Select Data Range with Duplicate values.", _ Default:=Selection.Address, Title:="Select Range", Type:=8) If dRange Is Nothing Then Exit Sub End If With dRange sRow = Cells(.Row, .Column).Row sCol = Cells(.Row, .Column).Column lCol = Cells(.Row, .Column + .Columns.Count - 1).Column lRow = Cells(.Row + .Rows.Count - 1, .Column + .Columns.Count - 1).Row End With dColumn = 0 Do While dColumn = 0 Set sCell = Application.InputBox(Prompt:= _ "Select Column with Duplicate values.", _ Title:="Select Column", Type:=8) On Error GoTo 0 If sCell Is Nothing Then Exit Sub End If If sCell.Columns.Count > 1 Then MsgBox ("Pls select Single Column only") Else dCol = sCell.Column If dCol < sCol Then MsgBox ("Select Column within earlier Range") ElseIf dCol > lCol Then MsgBox ("Select Column within earlier Range") Else dColumn = 1 End If End If Loop For cRow = lRow To sRow Step -1 If IsEmpty(Cells(cRow, dCol)) = False Then If WorksheetFunction.CountIf(Range(Cells(sRow, dCol), Cells(cRow, dCol)), Cells(cRow, dCol).Text) > 1 Then Range(Cells(cRow, sCol), Cells(cRow, lCol)).Select Selection.Interior.Color = 255 D = D + 1 End If End If Next cRow Cells(sRow, sCol).Select If D = 0 Then MsgBox ("No Duplicate Values Found") Else caution = MsgBox(D & " Duplicate entries selected and marked RED" & vbCrLf & _ "Do you want to delete them? " & vbCrLf & _ "Marked entries will be deleted and data will move up " & vbCrLf & _ "Do you want to Continue?", vbYesNo, "Confirmation") If caution = vbYes Then For cRow = lRow To sRow Step -1 If IsEmpty(Cells(cRow, dCol)) = False Then If WorksheetFunction.CountIf(Range(Cells(sRow, dCol), Cells(cRow, dCol)), Cells(cRow, dCol).Text) > 1 Then Range(Cells(cRow, sCol), Cells(cRow, lCol)).Select Selection.Delete Shift:=xlUp End If End If Next cRow Cells(sRow, sCol).Select End If End If
End Sub
|