Solved

Best way to get unique values in excel vba ?

Posted on 2010-11-30
9
1,118 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 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

19 Experts available now in Live!

Get 1:1 Help Now