Checking For Duplicates

kparadise

Board Regular
Joined
Aug 13, 2015
Messages
186
So I have a table of records. Each row is a record that contains 5 columns of items. I created these records manually, but now i need to identify any records (rows) that contain the same values across the 5 columns. See below: the first and third records are duplicates because they contain the same 5 value regardless of which column they fall in.

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]ITEM[/TD]
[TD]ITEM[/TD]
[TD]ITEM[/TD]
[TD]ITEM[/TD]
[TD]ITEM[/TD]
[TD]DUP?[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]apple[/TD]
[TD]orange[/TD]
[TD]grape[/TD]
[TD]dog[/TD]
[TD]cat[/TD]
[TD]Y[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]bug [/TD]
[TD]apple[/TD]
[TD]orange[/TD]
[TD]cat[/TD]
[TD]dog[/TD]
[TD]N[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]orange[/TD]
[TD]apple[/TD]
[TD]dog[/TD]
[TD]cat[/TD]
[TD]grape[/TD]
[TD]Y[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]dog[/TD]
[TD]bird[/TD]
[TD]banana[/TD]
[TD]cat[/TD]
[TD]grape[/TD]
[TD]N[/TD]
[/TR]
</tbody>[/TABLE]
 
Agreed, but based on this
Code:
the first and third records are duplicates because they contain the [B]same 5 value[/B] regardless of which column they fall in.
I read it that all 5 values must be in the same row.
But I could be wrong.

Hmmm yes you could be right.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Here is a VBA solution
- test in a COPY of your workbook
- assumes data is in columns A:E, with headers in row1

Place code in a module
Amend "SheetName"
(After testing) automatically delete temporary sheet by remove leading apostrophe
Code:
[COLOR=#ff0000][SIZE=3]'[/SIZE][/COLOR]temp.delete

Code:
Option Explicit

Sub TestForDuplicates()
    Application.ScreenUpdating = False
    Dim temp As Worksheet, ws As Worksheet, r As Long, c As Long, lastRow As Long, concat As String
    Set ws = Sheets("[COLOR=#ff0000]SheetName[/COLOR]")
'add temp sheet
    ws.Copy After:=Sheets(Sheets.Count)
    Set temp = Sheets(Sheets.Count)
'sort by row
    lastRow = temp.Range("A:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For r = 2 To lastRow: Call SortByRow(temp, r): Next
'concatenate cell text
    temp.Range("F1").ColumnWidth = 100
    For r = 2 To lastRow
        concat = ""
        For c = 1 To 5: concat = concat & "|" & temp.Cells(r, c): Next c
        temp.Cells(r, 6) = concat
    Next
'test for duplicates
    ws.Range("F2:F" & lastRow).Value = "N"
    For r = 2 To lastRow
        If WorksheetFunction.CountIf(temp.Range("F:F"), temp.Cells(r, 6)) > 1 Then ws.Cells(r, 6) = "Y"
    Next r
    Application.DisplayAlerts = False
    'temp.Delete  
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
Code:
Private Sub SortByRow(aSheet, aRow)
    Dim rng As Range
    With aSheet
        Set rng = .Cells(aRow, 1).Resize(, 5)
            .Sort.SortFields.Clear
            .Sort.SortFields.Add2 Key:=rng
            With .Sort
                .SetRange rng
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlLeftToRight
                .SortMethod = xlPinYin
                .Apply
            End With
    End With
End Sub
 
Last edited:
Upvote 0
Thanks yongle I only suggested a solution but as I was on my iPad a script wasn’t possible let alone tested
 
Upvote 0
ARRAY formula in F2 then copied down.

=IF(COUNTIFS(OFFSET($A$2:$E$2,ROW($A$2:$A$5)-ROW($A$2),),$A2:$E2)>0,"Y","N")

How ARRAY formula is entered


Paste Formula in the cell.
Press F2
Hold Shift+ Ctrl Keys and hit Enter key.
Now the formula is surrounded by {} by excel.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top