Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Best way to get unique values in excel vba ?

Posted on 2010-11-30
9
Medium Priority
?
1,135 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 1336 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:Tracy
Tracy earned 664 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 1336 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

572 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