Removing duplicates from multiple columns

etibor15

New Member
Joined
May 24, 2012
Messages
5
So i have about 1600 rows that I have transposed into columns and I need to remove all of the duplicates column by column (section example shown below).


732 370 699
636 785 235
636 565 869
636 565 699
287 100
287 643
287 816
816
248
579
703
703

Is there a way to do this with macros or VBA code?
Is there a a way to do it while they are still in row form?
 
The simple answer is yes, we can remove duplicates via VBA. For clarity's sake, let us say that 703 also existed in column 3 from your example data in post #1. Would we remove the last 703 in column 1 AND the 703 from column 3 (leaving only 1 value of 703) or just the last 703 in column 1?
 
Upvote 0
Each column needs to be treated individually. So there would be only one 703 in column 1 and one 703 in column 3.

It is as if you were to highlight column A and use the delete duplicates tool, but I need to do it over 1600 columns (or better yet rows since the column formation is just a transpose).

Thanks!
 
Upvote 0
Okay, a bit quickly written, but does this help?

Presume the yellow is selected (A2:D13):
Excel Workbook
ABCDEFGHI
1HdrHdrHdrHdrHdrHdrHdrHdr
2732370699732370699636
3636785235636636785235565
4636565869565287565869
5636565699816100
6287100248643
7287643579816
8287816703
9816
10248
11579
12703
13703
Sheet1
Excel 2010

In a Standard Module:
Rich (BB code):
Option Explicit
    
Sub example_RemoveDupsFromEaColOrRow()
Dim vntChoice As Variant
Dim bolChoice As Boolean
Dim aryVals() As Variant
    
    '// Just for the example, I might use a simple userform.                            //
    vntChoice = Application.InputBox( _
        "Enter ""r"" or ""1"" to remove duplicates from the rows selected." & vbCrLf & _
        "Enter ""c"" or ""2"" to remove duplicates from the columns selected.", _
            "Choose Operation", Type:=1 Or 2 _
                                     )
    '// Check to see that a valid pick was made or bailout.                             //
    If (vntChoice = vbNullString Or vntChoice = False) _
        Or Not InStr(1, "rc12", CStr(vntChoice)) > 0 _
        Or Selection.Cells.Count = 1 Then
        
        MsgBox "Bad or no entry; or only one cell selected...", vbInformation, vbNullString
        Exit Sub
    End If
    
    bolChoice = vntChoice = "c" Or vntChoice = 2
    aryVals = Selection.Value
    
    If RetUniques(aryVals, bolChoice) Then
        Range("F2").Resize(UBound(aryVals, 1), UBound(aryVals, 2)).Value = aryVals
    End If
End Sub
    
Function RetUniques(Vals() As Variant, Optional ByColumn As Boolean = False) As Boolean
Dim DIC         As Object ' Dictionary
Dim aryTemp     As Variant
Dim aryDicKeys  As Variant
Dim n           As Long
Dim x           As Long
Dim y           As Long
    
    '// Size an output array to equal the size of range selected.  I figure you will    //
    '// probably overwrite the range.                                                   //
    ReDim aryTemp(1 To UBound(Vals, 1), 1 To UBound(Vals, 2))
    Set DIC = CreateObject("Scripting.Dictionary")
    
    If Not ByColumn Then
        For x = 1 To UBound(Vals, 1)
            If DIC.Count > 0 Then DIC.RemoveAll
            For y = 1 To UBound(Vals, 2)
                DIC.Item(Vals(x, y)) = Empty
            Next
            If DIC.Exists(vbNullString) Then DIC.Remove (vbNullString)
            aryDicKeys = DIC.Keys
            For n = 0 To DIC.Count - 1
                aryTemp(x, n + 1) = aryDicKeys(n)
            Next
        Next
    Else
        For y = 1 To UBound(Vals, 2)
            If DIC.Count > 0 Then DIC.RemoveAll
            For x = 1 To UBound(Vals, 1)
                DIC.Item(Vals(x, y)) = Empty
            Next
            If DIC.Exists(vbNullString) Then DIC.Remove (vbNullString)
            aryDicKeys = DIC.Keys
            For n = 0 To DIC.Count - 1
                aryTemp(n + 1, y) = aryDicKeys(n)
            Next
        Next
    End If
    
    Vals = aryTemp
    RetUniques = True
End Function
Hope that helps,

Mark
 
Last edited:
Upvote 0
Glad that worked Elliot and thank you for the feed back :-)
 
Upvote 0
The simple answer is yes, we can remove duplicates via VBA. For clarity's sake, let us say that 703 also existed in column 3 from your example data in post #1 . Would we remove the last 703 in column 1 AND the 703 from column 3 (leaving only 1 value of 703) or just the last 703 in column 1?
HI I know this is late but do you know how to code such that the first duplicate found is deleted and one remains?
 
Upvote 0

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