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
|
How do you remove both the original and the duplicate?
ReplyDeleteYogesh,
ReplyDeleteI tried running your macro to delete rows with duplicate data in the column which contains "Description" i.e. long sentences. It gives me an errors "Run-time error 1004, Unable to get the CountIf property of the WorksheetFunction class". Do you know how to resolve this. The macro runs fine if I try to remove duplicates with minimum string length, but to remove duplicate sentences, it gives the above error.
Thanks,
Robin
Looks like there's a minor change required for the first macro. The alert should read "Select Starting Cell For Column with Duplicate values." because it's looking for a cell reference, not the row #
ReplyDeleteThis is an amazing Macro, just not exactly what i need. I am trying to change (Sorry I am a total n00b with macros)it and make it delete all the duplicated, but the last one. I am doing a list of IDs and years but i need to delete all dublicated reports, but the last one. Can anyone help me, I have already tried changing the For order so go from first to last i guess it is ok this way:
ReplyDeleteFor cRow=sRow to lRow step 1
Next cRow
But actually it didnt change anything at all help me please
In the example it has
ReplyDelete1 AA
2 AA
Well, I need you help to make it delete the 1 AA instead of the 2 AA
11 FF
12 FF
13 FF
14 FF
15 FF I want to all the previous be deleted expet the last one, please help i just coudlnt figure it out
removing duplicate rows ..your macro helped me a lot,
ReplyDeletethanks you
AMIT TARALI
I want to convert this chart:
ReplyDeleteName People
XYZ KK
XYZ KY
ABC LL
ABC LY
BBC XY
BBC XY
CBA XX
DBC XX
DDD XX
DDD XY
DDD XX
to this chart:
XYZ KK KY
ABC LL LY
BBC XY XY
CBA XX
DBC XX
DDD XX XY XX
Is there any macro for that?