Solved

Best way to get unique values in excel vba ?

Posted on 2010-11-30
9
1,121 Views
Last Modified: 2012-05-10
Hello!

I am working in excel 2007, in vba.

I have a range of cells that contain various values / data. I want to exctract only the unique values/occurences and remove any duplicates.

My ideal "result" would be an array holding each unique value with the data type of that value preserved.

I have seen similar solutions using user functions, or specialized versions of the advanced filter from vba.
But my question is... which way is the best way?
and by best I mean that it fulfills these criteria:
- as fast as possible (avoiding any unnessecary looping etc)
- can accept any value types (dates, strings, numbers whatever...)
- can be fully used from vba

I have found some functions, for example:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_24147270.html?sfQueryTermInfo=1+10+30+excel+find+uniqu+valu+vba

but it seems to be written specifically to work on string values, and gives unexpected results when I pass a range of cells holding date data type.

So how should I do it?
Advanced filter? or custom function? or some other way?
regardless of which, could you please provide me with an example including code?
thanks!
0
Comment
Question by:NiklasMoller
9 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 334 total points
ID: 34240621
Advanced Filter is pretty good. So if you had data in A1 and down this would give you a list of unique values in G1 and down:
Sub Macro2()

Range("A1", Range("A1").End(xlDown)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("G1"), Unique:=True

End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:broomee9
broomee9 earned 166 total points
ID: 34240635
I think the best way to do this is to use the Dictionary class.  You can store unique values with different data types in a dictionary.

Have a look at Patrick's article (complete with code) on this and the difference between a Collection, which can only hold a single data type:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 34240724
Dictionary is very useful. I seem to recall having some problems storing numerical entries at one point, but could have been my ineptitude.
0
 
LVL 13

Expert Comment

by:gbanik
ID: 34240750
Here is one of the way... pass your range to this subroutine to remove duplicates (assumption made is your range is in ONE column)

Run the macro Test using CRTL + SHFT + T
Option Explicit

Public Sub Test()
DeleteDuplicates Application.Range("A1:A140")
End Sub

Public Sub DeleteDuplicates(oRange As Range)
Dim nStart As Long, nEnd As Long, sCol As String, oDelRange As Range, oCell As Range
Application.ScreenUpdating = False
Set oRange = oRange.Columns(1)
oRange.Offset(, 1).EntireColumn.Insert
nStart = oRange.Row
nEnd = oRange.Rows(oRange.Rows.Count).Row
sCol = Split(oRange.Cells(1).Address, "$")(1)
oRange.Offset(, 1).Formula = "=IF(COUNTIF(" & sCol & "$" & nStart & ":" & sCol & nStart & "," & sCol & nStart & ")=COUNTIF(" & sCol & "$" & nStart & ":" & sCol & "$" & nEnd & "," & sCol & nStart & "),0,1)"
For Each oCell In oRange.Cells
    If oCell.Offset(, 1).Value = 1 Then
        If oDelRange Is Nothing Then
            Set oDelRange = oCell.Resize(, 2)
        Else
            Set oDelRange = Union(oDelRange, oCell.Resize(, 2))
        End If
    End If
Next
oDelRange.Delete xlShiftUp
oRange.Offset(, 1).EntireColumn.Delete
Application.ScreenUpdating = True
End Sub

Open in new window

Remove-Duplicates.xlsm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 24

Expert Comment

by:StephenJR
ID: 34240761
Just tested it and it all seems fine so not sure what I was doing before.
0
 
LVL 5

Author Comment

by:NiklasMoller
ID: 34243950
Thanks for many interesting answers!
@stephenjr:
You mention using the Dictionary class. Could you give me some code that adresses my problem using that class? Would it not mean that i would have to loop over all the values and do some kind of comparison? Is looping all cells really quicker than advanced filter? Thanks!
0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 334 total points
ID: 34244020
Actually broomee9 mentioned Dictionary and you should read the article referred to. Here is a simple example - press the button in the attachment. Transferring the cells into an array speeds things up.
Sub x()

Dim oDic As Object, vData As Variant, n As Long, r As Long

vData = Range("A1", Range("A1").End(xlDown)).Value
Set oDic = CreateObject("Scripting.Dictionary")

With oDic
    For r = 1 To UBound(vData, 1)
        If Not IsEmpty(vData(r, 1)) And Not .Exists(vData(r, 1)) Then
            n = n + 1
            .Add vData(r, 1), n
        End If
    Next r
    Range("G1").Resize(.Count) = Application.Transpose(.Keys)
End With

End Sub

Open in new window

Book1.xlsm
0
 
LVL 5

Author Comment

by:NiklasMoller
ID: 34244051
Yes of course,sorry i got it backwards :) i will try your script and i will read that article! Get back to you all soon! Thanks
0
 
LVL 5

Author Comment

by:NiklasMoller
ID: 34252939
ok heres a status update.
@Stephen JR
I tried your provided sub, and I tried to measure the time it took to loop through 100,000 cells in one column containing random numbers
Printing the numbers using a for loop took about 5 seconds.
Extracting the unique values and printing them to the G column takes less than 0,5 seconds!
almost instantaneous! so thats good :)

and that is 100,000 cells, I will rarely approach those masses of cells.
so anything less than less than that will be close to lightning fast!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now