Get Unique Array From an Array


Function GetUnique(InputArray) As Variant
    ‘==========================================
    ‘Created on 13-Aug-2012
    ‘Function Return an Array of Unique Values
    ‘==========================================
    Dim lngRow      As Long
    Dim lngCol      As Long
    Dim ResArray
    With CreateObject(“Scripting.Dictionary”)
        If IsArray2Dimensional Then
            For lngRow = LBound(InputArray) To UBound(InputArray)
                For lngCol = LBound(InputArray) To UBound(InputArray, 2)
                    .Item(InputArray(lngRow, lngCol)) = 0
                Next lngCol
            Next lngRow
        Else
            For lngRow = LBound(InputArray) To UBound(InputArray)
                .Item(InputArray(lngRow)) = 0
            Next lngRow
        End If
    GetUnique = .keys
    End With
End Function
Function IsArray2Dimensional(InputArray) As Boolean
    ‘================================================
    ‘       Created by rajan on 20-July
    ‘       this function check is Array 2D or not
    ‘                    ParameterInfo
    ‘       InputArray   :   Array you want to check
    ‘================================================
    On Error GoTo ExitEarly:
    If UBound(InputArray, 2) > 1 Then IsArray2Dimensional = True
ExitEarly:
    If Err.Number <> 0 Then IsArray2Dimensional = False
End Function

2 Comments Add yours

  1. This change works with any number of array dimensions.

    Public Function GetUnique(vArray As Variant) As Variant

    ‘ Created by: C.Hatmaker

    Dim vElement As Variant

    With CreateObject(“Scripting.Dictionary”)
    For Each vElement In vArray
    .Item(vElement) = 0
    Next
    GetUnique = .Keys
    End With

    End Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s