cellMatrix.net
Spreadsheet Modeling and Related Topics

About

Formulas, Charts, and Models Created with Microsoft Excel.
More . . .

Statistics

  • Total Entries - 102
  • Current Viewers - 13

Categories

Recent Comments

Syndicate

Validate

Extract Unique Values in a Range

Depending on how your data is grouped there are a series of options available in Excel to capture unique values. You can do so with filters, formulas, and VBA.

The macro below captures unique values from a range that spans multiple rows and columns. In the example, the source range is "A1:B20" and the output range starts with cell "D1". The macro works because, by definition, the collection variable will only accept unique values.

Sub ListUnique()

    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim Cell As Range
    Dim UniqueValues As New Collection

    Set Rng1 = Range("A1:B20")
    Set Rng2 = Range("D1")

    On Error Resume Next
    For Each Cell In Rng1
        UniqueValues.Add Cell.Value, CStr(Cell.Value)
    Next Cell

    For Each Item In UniqueValues
        Rng2.Value = Item
        Set Rng2 = Rng2.Offset(1, 0)
    Next Item

End Sub



Comment Form

Name: (Required)
E-Mail Address: (Optional)
Location: (Optional)
Web Site Address: (Optional)

Remember my personal information?
Notify me of follow-up comments?

Before submitting your comment, please enter the phrase you see below: