I have seen that some experts have written articles on how to handle duplicate rows in Excel, such as:
=COUNTIFS(A:A,A2,B:B,B2,C:C,C2,D:D,D2,E:E,E2,F:F,F2)
We can drag the formula down and you should see the result as below:
If the criteria argument is a reference to an empty cell, the COUNTIFS function treats the empty cell as a 0 value.To resolve this issue, we would change the formula to:
=COUNTIFS(A:A,""&A2,B:B,""&B2,C:C,""&C2,D:D,""&D2,E:E,""&E2,F:F,""&F2)
We will see the zero issue is now being resolved.
=COUNTIFS(A:A,""&A2,B:B,""&B2,C:C,""&C2,D:D,""&D2,E:E,""&E2,F:F,""&F2)>1
We can do the same by dragging the formula down.
=$G2=FALSE
=$A$2:$F$5000
=$G2=TRUE
And do remember to update the Applies to range as well.
=$A$2:$F$5000
to:
=$A$2:$F$25
=AND($G2=FALSE,$G2<>"")
For Red rule, use:
=AND($G2=TRUE,$G2<>"")
As of now, it looks pretty well a good job for us.
=AND(COUNTIFS($A$2:$A$5000,""&$A2,$B$2:$B$5000,""&$B2,$C$2:$C$5000,""&$C2,$D$2:$D$5000,""&$D2,$E$2:$E$5000,""&$E2,$F$2:$F$5000,""&$F2)=1,$A2&$B2&$C2&$D2&$E2&$F2<>"")
=AND(COUNTIFS($A$2:$A$5000,""&$A2,$B$2:$B$5000,""&$B2,$C$2:$C$5000,""&$C2,$D$2:$D$5000,""&$D2,$E$2:$E$5000,""&$E2,$F$2:$F$5000,""&$F2)>1,$A2&$B2&$C2&$D2&$E2&$F2<>"")
Sub test()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Call Dedup(ws)
End Sub
Sub Dedup(ws As Worksheet)
ws.Range("$A2:$F$5000").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6), Header:=xlYes
End Sub
Sub test()
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Call ApplyFilter(ws, "G", True)
Call RemoveHiddenRows(ws)
Call RemoveFilter(ws)
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Sub ApplyFilter(ws As Worksheet, Col As String, Value As Variant, Optional rng As String = "A1")
ColIndex = Range(Col & 1).Column
ws.Range(rng).AutoFilter Field:=ColIndex, Criteria1:=Value
End Sub
Sub RemoveFilter(ws As Worksheet, Optional rng As String = "A1")
ws.Range(rng).AutoFilter
End Sub
Sub RemoveHiddenRows(ws As Worksheet, Optional rng As String = "A2")
Dim r As Range, Row As Range, LastRow As Integer
Set r = ws.Range(rng)
LastRow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
Do Until r.Row > LastRow
If r.EntireRow.Hidden = True Then
If Row Is Nothing Then
Set Row = r.EntireRow
Else
Set Row = Union(Row, r.EntireRow)
End If
End If
Set r = r.Offset(1, 0)
Loop
If Not Row Is Nothing Then
Row.Delete
End If
End Sub
After you run the codes, you will get the result as follows:
Sub CreateFormula(ws As Worksheet, Col As String)
Dim idx As Integer, LastRow As Integer, formulaStr As String
idx = Range(Col & "1").Column
For i = 1 To idx
formulaStr = IIf(i = idx, "", ",") & "C[-" & i & "],""""&RC[-" & i & "]" & formulaStr
Next
LastRow = ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row
ws.Range(Col & "2:" & Col & LastRow).FormulaR1C1 = "=COUNTIFS(" & formulaStr & ")>1"
End Sub
Sub DeleteColumn(ws As Worksheet, Col As String)
ws.Columns(Col).EntireColumn.Delete
End Sub
To call the functions:
Sub test2()
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Call CreateFormula(ws, "G")
Call ApplyFilter(ws, "G", True)
Call RemoveHiddenRows(ws)
Call RemoveFilter(ws)
Call DeleteColumn(ws, "G")
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Sub test()
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Call ApplyFilter(ws, "G", False)
Call RemoveHiddenRows(ws)
Call RemoveFilter(ws)
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Sub test2()
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Call CreateFormula(ws, "G")
Call ApplyFilter(ws, "G", False)
Call RemoveHiddenRows(ws)
Call RemoveFilter(ws)
Call DeleteColumn(ws, "G")
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Function CreateSheet(wb As Workbook, wsName As String) As Worksheet
Dim ws_target As Worksheet
Set ws_target = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count))
ws_target.Name = "Output"
Set CreateSheet = ws_target
End Function
Function DeleteSheet(wb As Workbook, wsName As String) As Boolean
Dim Sheet As Worksheet
For Each Sheet In wb.Worksheets
If UCase(Sheet.Name) = UCase(wsName) Then
Application.DisplayAlerts = False
Sheet.Delete
Application.DisplayAlerts = True
DeleteSheet = True
Exit Function
End If
Next
DeleteSheet = False
End Function
Sub MoveFilteredRows(ws_source As Worksheet, ws_target As Worksheet)
ws_source.UsedRange.SpecialCells(xlCellTypeVisible).Copy
With ws_target.Cells(1, 1)
.PasteSpecial
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
End With
Application.CutCopyMode = False
ws_target.Cells(1, 1).Select
End Sub
Sub ClearContent(ws As Worksheet, Col As String)
ws.Columns(Col & ":" & Col).ClearContents
End Sub
To test the codes, we would need test3 procedure:
Sub test3()
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim wb_target As Workbook
Dim ws_source As Worksheet, ws_target As Worksheet
Set ws_source = Sheets("Sheet1")
Set wb_target = ws_source.Parent
Call DeleteSheet(wb_target, "Output")
Set ws_target = CreateSheet(wb_target, "Output")
Call CreateFormula(ws_source, "G")
Call ApplyFilter(ws_source, "G", True)
Call ClearContent(ws_source, "G")
Call MoveFilteredRows(ws_source, ws_target)
Call RemoveFilter(ws_source)
Call DeleteColumn(ws_source, "G")
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Once you run the codes above, you can see a new sheet named as "Output" being generated with all duplicate rows.
Sub test4()
With Application
.EnableEvents = False
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim wb_target As Workbook
Dim ws_source As Worksheet, ws_target As Worksheet
Set ws_source = Sheets("Sheet1")
Set wb_target = ws_source.Parent
Call DeleteSheet(wb_target, "Output")
Set ws_target = CreateSheet(wb_target, "Output")
Call CreateFormula(ws_source, "G")
Call ApplyFilter(ws_source, "G", False)
Call ClearContent(ws_source, "G")
Call MoveFilteredRows(ws_source, ws_target)
Call RemoveFilter(ws_source)
Call DeleteColumn(ws_source, "G")
With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Advanced Filter can also be used to either copy the unique rows to another location or in place in case there are duplicate rows in the data set.
Author
Commented: