Removing and finding the Duplicates from arrays or excel sheet Rows entries

mandukes

Forum Rules
Joined
May 25, 2013
Messages
90
I need to remove duplicates from rows in excel sheet and also find the no of duplicates found in the current row.
[TABLE="width: 100"]
<tbody>[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]5[/TD]
[TD]1[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]5[/TD]
[TD]4[/TD]
[TD]6[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]9[/TD]
[TD]5[/TD]
[TD]4[/TD]
[TD]9[/TD]
[/TR]
</tbody>[/TABLE]

Output:

1 2 5 3
6 5 4
6 9 5 4

Can it be achieved using an arrays.

Code:
Dim ArrItem(100,50) as Integer 
ArrItems(1) = ArrItems(1,2,5,1,3)
ArrItems(2) = ArrItems(6,5,4,6,5)
ArrItems(3) = ArrItems(6,9,5,4,9)

Thanks and regards
~M
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
.. and also find the no of duplicates found in the current row.
1. Where do you want recorded the number of duplicates found in each row?

2. How do you count duplicates? That is, in the sample below, does this row have 2 duplicates (1 and 2) or does it have 3 duplicates (1, 2 and 1)?


Book1
ABCDEFG
21213214
Dupes


3. Do all rows have the same number of values to start with (5 in your example)?
 
Last edited:
Upvote 0
1. Where do you want recorded the number of duplicates found in each row?

Before Each row.

For example:
If 3 dupes found in the following.

DupesBCDEFG

<tbody>
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]

</tbody>

The dupe should be removed from the Row i.e. 1,2,1
Giving the output as:

Output:
DupesBCDEFG

<tbody>
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]

</tbody>


2. How do you count duplicates? That is, in the sample below, does this row have 2 duplicates (1 and 2) or does it have 3 duplicates (1, 2 and 1)?

ABCDEFG

<tbody>
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]4[/TD]

</tbody>


In your example given above.
It has 3 duplicates (1, 2 and 1)


3. Do all rows have the same number of values to start with (5 in your example)?

No, it can have more than 5 upto 100


Thanks :)
 
Last edited:
Upvote 0
Hi
Wasn't sure if you wanted to overwrite your data or copy the info to a new sheet.
This will write the unique values & the number of duplicates to a sheet called new.
You'll need to change the sheet name in red to match your data sheet.
Code:
Sub Del_DupesInRw()
' mandukes

    Dim Cl As Range
    Dim Rw As Long
    Dim Dupes As Long
    
Application.ScreenUpdating = False

    On Error Resume Next
    Sheets.Add.Name = "New"
    If Err.Number Then ActiveSheet.Delete
    On Error GoTo 0
    
    Sheets("[COLOR=#ff0000]overall[/COLOR]").Activate
    With CreateObject("scripting.dictionary")
        For Rw = 1 To Range("A" & Rows.Count).End(xlUp).Row
            For Each Cl In Range("A" & Rw, Cells(Rw, Columns.Count).End(xlToLeft))
                If Not .exists(Cl.Value) Then
                    .Add Cl.Value, Nothing
                Else
                    Dupes = Dupes + 1
                End If
            Next Cl
            Sheets("New").Rows(Rw).ClearContents
            Sheets("New").Range("A" & Rw) = Dupes
            Sheets("New").Range("B" & Rw).Resize(, .Count) = .keys
            Dupes = 0
            .removeall
        Next Rw
    End With
    
End Sub
 
Upvote 0
Thanks for the reply,
the code worked , The problem has been solved, I want to close this thread but couldn't find the option to do so..
 
Upvote 0
I want to close this thread but couldn't find the option to do so..
We don't have such an option in this forum because even though an OP (you) may be satisfied with one of the responses, it is not uncommon for somebody to come along later and offer an even better solution that you, or other readers, may want to use instead.
 
Upvote 0
Thanks for the reply,
the code worked , The problem has been solved, I want to close this thread but couldn't find the option to do so..

Glad to help &thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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