function for Unique values in an array

ankita.sethi

Board Regular
Joined
Apr 27, 2011
Messages
58
Hi,

I have an array called "Universe", which have duplicated values.
I am trying to write a code that gives me an array which have only unique values.

Can anyone help?
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi

You can use a dictionary object to populate a unique array (it has an Exists method which can check for duplicates):

Code:
Dim cell As Range
Dim dic As Object
Dim myUniqueArray As Variant

Set dic = CreateObject("Scripting.Dictionary")

'use it to load the dictionary with all the unique values in A1:A1000:

For Each cell In Range("A1:A1000")
  If Not dic.Exists(cell.Value) Then _
      dic.Add cell.Value, cell.Value
Next cell


'output to another range:
myUniqueArray = dic.Keys
Range("B1").Resize(UBound(myUniqueArray) - LBound(myUniqueArray) + 1) = Application.Transpose(myUniqueArray)
 

ankita.sethi

Board Regular
Joined
Apr 27, 2011
Messages
58
Thanks for the reply.

If I am understanding correctly, this is taking values from a range in the sheet. However, I have an array defined in the VBA itself and I want the result also as a VBA array. Am i making sense?
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
You can take the values from the array instead of from the sheet:

Code:
For i = LBound(myArray) to Ubound(myArray)
  If Not dic.Exists(myArray(i)) Then _
     dic.Add myArray(i), myArray(i)
Next i

myUniqueArray = dic.Keys   'you now have your array with no duplicates
Make sense?
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
@Richard Schollar:

Hi Richard :)

Just curious as to why the IF .Exists test when only taking the keys?

Mark
 

ankita.sethi

Board Regular
Joined
Apr 27, 2011
Messages
58
Thanks a lot!
This works great!
Could you please also tell me how can I see what value an array has at some time in the code like we do for variables either by placing the cursor or in the immediate window?
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
As long as the array is declared in the procedure running, you can use the Locals window to view variables' values while stepping thru the code.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hey Mark

Do you mean instead of something like this:

Code:
For i = 1 to 10
  dic.Item(myArray(i)) = 1
Next i
If so, no real reason. I just find the code I originally posted a lot more intuitive at first glance (and thus easier for me to remember! ;))
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
An overdue Howdy from out west and yes sir, that was what I was wondering.

Thank you,

Mark
 

ankita.sethi

Board Regular
Joined
Apr 27, 2011
Messages
58
Hi,
Thanks a lot again :)
The code works really well. Can you please explain the intuition behind it?What is dictionary object?What does it do?
 

Watch MrExcel Video

Forum statistics

Threads
1,099,013
Messages
5,466,023
Members
406,461
Latest member
Garrus

This Week's Hot Topics

Top