Eliminate Duplicates in Arrays

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
Suppose I have 2 Arrays: Example1(1 to 20) and Example2(1 to 35)

Is there a quick an easy was to compare these two and remove any records that are in BOTH of the arrays?

Also, the "20" and "35" will vary each time the macro is executed.
 
The original arrays contained duplicates and also in the combination of the 2 arrays have duplicates.
Ik did not remove the duplicates from the original arrays.
When you click the button: Merge.... you will see the result of the merged arrays in column C without duplicates.
Did you click the button ?

Yes, when I worked with the macro, it did merge the two without any duplication. ;)
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Thank you trunten. I did not know about this reasoning. Got something new to learn.

I have revised code to use Dictionary Object.
Code:
Sub UniqueArray()
Dim a(), b()
Dim objDn As Object

'Two Arrays
a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
b = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35)

Set objDn = CreateObject("Scripting.Dictionary")

'Here we load up the third one from first 2
For i = LBound(b) To UBound(b)
    If Not IsNumeric(Application.Match(b(i), a, 0)) And Not (objDn.Exists(b(i))) Then
    objDn.Add b(i), b(i)
    End If
Next i

'Printing the list
MsgBox "Total Items Found :" & (objDn.Count) & ". They Are:" & vbCrLf & Join(objDn.keys, vbCrLf)
End Sub
 
Upvote 0
Is this useful?
Code:
Sub UniqueArray()
Dim a(), b(), c(), iCount As Long
'Two Arrays
a = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
b = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35)
 
'Here we load up the third one from first 2
For i = LBound(b) To UBound(b)
    If Not IsNumeric(Application.Match(b(i), a, 0)) Then
    ReDim Preserve c(iCount)
    c(iCount) = b(i)
    iCount = iCount + 1
    End If
Next i
 
'Printing the list
MsgBox "Total Items Found :" & (UBound(c) + 1) & ". They Are:" & vbCrLf & Join(c, vbCrLf)
End Sub

Yes, that is useful. However, since I am a neophyte with arrays, how would I fill the 'a' array with the values in A2:A20, for example? :confused:
 
Upvote 0
Thank you trunten. I did not know about this reasoning. Got something new to learn.

:) Np. Glad you took it in the spirit it was meant too.
I'm quite fond of ArrayLists tbh, easy to get an array from them if need be, indexable, unordered and no limits.
 
Upvote 0
To work with ranges you can do something like.
Code:
Sub UniqueArray()
Dim r As Range
Dim objDn As Object

Set objDn = CreateObject("Scripting.Dictionary")

For Each r In Range("B1:B35") 'Set Larger Range Reference Here
    If Not IsNumeric(Application.Match(r.Value, Range("A1:A20"), 0)) _
    And Not objDn.exists(r.Value) Then 'Set Smaller Range Reference Here
    objDn.Add r.Value, r.Value
    End If
Next r

'Printing the list
MsgBox "Total Items Found :" & (objDn.Count) & ". They Are:" & vbCrLf & Join(objDn.keys, vbCrLf)
End Sub
 
Upvote 0
To work with ranges you can do something like.
Code:
Sub UniqueArray()
Dim r As Range
Dim objDn As Object
 
Set objDn = CreateObject("Scripting.Dictionary")
 
For Each r In Range("B1:B35") 'Set Larger Range Reference Here
    If Not IsNumeric(Application.Match(r.Value, Range("A1:A20"), 0)) _
    And Not objDn.exists(r.Value) Then 'Set Smaller Range Reference Here
    objDn.Add r.Value, r.Value
    End If
Next r
 
'Printing the list
MsgBox "Total Items Found :" & (objDn.Count) & ". They Are:" & vbCrLf & Join(objDn.keys, vbCrLf)
End Sub

OK, one last question. How do I get the objDn results to populate into cells, rather than a msgbox?
 
Upvote 0
Just change the last line like:
Code:
Range("C1").Resize(objDn.Count, 1).Value = Application.Transpose(objDn.keys)
 
Upvote 0
OK, one last question. How do I get the objDn results to populate into cells, rather than a msgbox?


Hi,

In Taurean code replace:

Code:
 MsgBox "Total Items Found :" & (objDn.Count) & ". They Are:" & vbCrLf & Join(objDn.keys, vbCrLf)

With:
Code:
 Range("C2:C" & objDn.Count) = Application.Transpose(Array(objDn.keys))
 
Upvote 0

Forum statistics

Threads
1,223,978
Messages
6,175,755
Members
452,667
Latest member
vanessavalentino83

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