Search code examples
vbadateexcelunique

find unique dates in excel vba array


I've got a data set that contains multiple measurements for each day. I've already put the data in a VBA array. Now, I would like to create an array with the unique days.

_Here is my dataset:_  
12/07/2010     3.5

12/07/2010     2.1

12/07/2010     2.2

12/08/2010     5.2

12/08/2010     3.2

12/09/2010     4.7

Here is what I would like to return:

12/07/2010

12/08/2010

12/09/2010

I've looked at a few user defined functions for unique values, but they operate on numerical values, not date types.

Any help is greatly appreciated. Thanks!


Solution

  • The Date type is a number...

    An easy way is to use a collection:

    Sub Uniques()
        Dim oColl As New Collection
    
        Dim vArr As Variant
        Dim vItem As Variant
        Dim j As Long
        vArr = Range("A1:B6")
        On Error Resume Next
        For j = LBound(vArr) To UBound(vArr)
            oColl.Add vArr(j, 1), CStr(vArr(j, 1))
        Next j
        On Error GoTo 0
    
        For Each vItem In oColl
            Debug.Print vItem
        Next vItem
    End Sub