Extract only unique values from a range in Excel

Posted on 2011-05-12
Medium Priority
Last Modified: 2012-05-11
I have a range of about 7 values in  a column, but some of hte values will repeat.  I need to pull outonly unqiue values and use these in a loop.  For whatever reason, the Data Advance Filter Unique values in Excel 2003 does not work.  Is there any way to do this otherwise?  Has to be done with VBA
Question by:ssmith94015
  • 2
  • 2
LVL 93

Expert Comment

by:Patrick Matthews
ID: 35750601
The advanced filter should work. If it doesn't that would indicate that the values are not really distinct.

Are there perhaps some stray characters in the entries, such as trailing spaces?

When you tried the adv filter, did you perhaps select >1 column?

Author Comment

ID: 35751050
That was the first thing I checked.  In code and manully, still returns duplicates.  
LVL 93

Expert Comment

by:Patrick Matthews
ID: 35751606

It would be useful to see some sample data and/or a sample file.  EE now allows you to directly upload files to your question.

Please be advised that once you upload a file, it can be publicly accessed, and that it may not be possible to fully and permanently delete it.  The file may also be indexed by the major search engines.

Therefore, be very careful about posting proprietary, confidential, or other sensitive information.  If necessary, use "fake" and/or obfuscated data in your sample.

Please note that at present EE restricts uploads to certain file types.  If your file type does not match those in the list, you can use http://www.ee-stuff.com instead, which is not officially an EE site, but is run by people connected to EE.

LVL 18

Accepted Solution

krishnakrkc earned 2000 total points
ID: 35752289

Try this function.

Function GETUNIQUE(ByRef r As Range)
    Dim i   As Long, c As Long, d
    If TypeOf r Is Range Then
        d = r.Value2
    Else: Exit Function
    End If
    With CreateObject("scripting.dictionary")
        .comparemode = 1
        If IsArray(d) Then
            For i = 1 To UBound(d, 1)
                For c = 1 To UBound(d, 2)
                    If Len(d(i, c)) Then
                        .Item(d(i, c)) = Empty
                    End If
            GETUNIQUE = .keys
            GETUNIQUE = d
        End If
    End With
End Function

'and call the function like

Sub kTest()
Dim a
a = GETUNIQUE(Range("a2:a10"))
MsgBox Join(a, vbLf)
End Sub

Open in new window


Author Closing Comment

ID: 35771464
I am so sorry I did not get back to this.  My contract is up in two days so have been frantically working on other issues.  Thank you for the help as this really was a stumbling block.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

757 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