• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Extract only unique values from a range in Excel

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
Sandra Smith
Sandra Smith
  • 2
  • 2
1 Solution
Patrick MatthewsCommented:
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?
Sandra SmithRetiredAuthor Commented:
That was the first thing I checked.  In code and manully, still returns duplicates.  
Patrick MatthewsCommented:

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.


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

Sandra SmithRetiredAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now