Remove duplicates, return multiple columns

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code will remove duplicates from column A, so if my data was:

Code:
a
a
b
c
d

it will return:

Code:
a
b
c
d

Here is the code:

Code:
    Dim DIC As Object
    Set DIC = CreateObject("Scripting.Dictionary")

    
    Dim MyArray() As Variant

    MyArray() = Sheet1.Cells(1, 1).CurrentRegion.Value

    
    Dim n As Long


    For n = 1 To UBound(MyArray(), 1)

        DIC.Item(MyArray(n, 1)) = 0

    Next n


    MyArray() = DIC.Keys


    Dim NewArray() As Variant
    ReDim NewArray(1 To DIC.Count - 1) As Variant


    Dim a As Long


    For a = 1 To DIC.Count - 1

        NewArray(a) = MyArray(a)

    Next a

But how can I adapt it so that if my data contains 2 columns, that it looks for duplicates in column A and then returns BOTH columns of data, eg

Code:
a 1
a 2
b 3
c 4
d 5

and I want it to return:

Code:
a 1
b 3
c 4
d 5

Thanks
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Maybe
Code:
Sub tiredofit()
   Dim Dic As Object
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Sheet2.Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   For i = 1 To UBound(Ary)
      Dic.Item(Ary(i, 1)) = Ary(i, 2)
   Next i
   ReDim Nary(1 To Dic.Count, 1 To 2)
   For i = 0 To Dic.Count - 1
      Nary(i + 1, 1) = Dic.Keys()(i)
      Nary(i + 1, 2) = Dic.Items()(i)
   Next i
   
End Sub
 
Upvote 0
Maybe
Code:
Sub tiredofit()
   Dim Dic As Object
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Sheet2.Range("A1").CurrentRegion.Value2
   Set Dic = CreateObject("scripting.dictionary")
   For i = 1 To UBound(Ary)
      Dic.Item(Ary(i, 1)) = Ary(i, 2)
   Next i
   ReDim Nary(1 To Dic.Count, 1 To 2)
   For i = 0 To Dic.Count - 1
      Nary(i + 1, 1) = Dic.Keys()(i)
      Nary(i + 1, 2) = Dic.Items()(i)
   Next i
   
End Sub

Thanks

I did it like this:

Code:
    Dim DIC As Object
    Set DIC = CreateObject("Scripting.Dictionary")

    
    Dim MyArray() As Variant

    MyArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
    
    Dim MyArrayRows As Integer
    
    MyArrayRows = UBound(MyArray(), 1)
    
    Dim MyArrayCols As Integer
    
    MyArrayCols = UBound(MyArray(), 2)
    
    Dim UniqueArray() As Variant
    ReDim UniqueArray(1 To MyArrayRows, 1 To MyArrayCols) As Variant
    
    Dim n As Long
    
    Dim i, j
    
    i = 1
    
    On Error GoTo ErrHandle
    
    For n = 1 To UBound(MyArray(), 1)
        
        DIC.Add MyArray(n, 1), MyArray(n, 1)
            
        For j = 1 To MyArrayCols
        
            UniqueArray(i, j) = MyArray(n, j)
            
            
        Next j
        
        i = i + 1
        
Cont:


    Next n
        
    Exit Sub
    
    
Errhandle:
    
    Resume Cont

so when there's a duplicate, I handle the error, else I loop through another array.
 
Upvote 0
Personally I'd get rid of the error handler & do it like
Code:
   For n = 1 To UBound(MyArray(), 1)
      If Not Dic.Exists(MyArray(n, 1)) Then
         Dic.Add MyArray(n, 1), MyArray(n, 1)
         For j = 1 To MyArrayCols
            UniqueArray(i, j) = MyArray(n, j)
         Next j
         i = i + 1
      End If
   Next n
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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