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?
 

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?
 

Forum statistics

Threads
1,082,318
Messages
5,364,528
Members
400,804
Latest member
davileal

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top