Consulting

Results 1 to 2 of 2

Thread: help with uniquevalue function

  1. #1

    help with uniquevalue function

    hi all,

    I got the function, shown below, from here and would like to load the unique values into an array but keep receiving type mismatch errors. Please let me know if this possible and how to make it work.

    Thanks

    Option Base 1Function UniqueItems(ArrayIn, Optional Count As Variant) As Variant
    '   Accepts an array or range as input
    '   If Count = True or is missing, the function returns the number of unique elements
    '   If Count = False, the function returns a variant array of unique elements
        Dim Unique() As Variant ' array that holds the unique items
        Dim Element As Variant
        Dim i As Integer
        Dim FoundMatch As Boolean
    '   If 2nd argument is missing, assign default value
        If IsMissing(Count) Then Count = True
    '   Counter for number of unique elements
        NumUnique = 0
    '   Loop thru the input array
        For Each Element In ArrayIn
            FoundMatch = False
    '       Has item been added yet?
            For i = 1 To NumUnique
                If Element = Unique(i) Then
                    FoundMatch = True
                    Exit For '(exit loop)
                End If
            Next i
    AddItem:
    '       If not in list, add the item to unique list
            If Not FoundMatch And Not IsEmpty(Element) Then
                NumUnique = NumUnique + 1
                ReDim Preserve Unique(NumUnique)
                Unique(NumUnique) = Element
            End If
        Next Element
    '   Assign a value to the function
        If Count Then UniqueItems = NumUnique Else UniqueItems = Unique End Function
    edit - I solved this by declaring the "unique" variable as public.

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    attach please a sample file to see how you are using it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •