Unique Function UDF Issue

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
An Excel 2003 udf for unique items works fine for 2 or greater than two unique items in a list of repeated items. BUT if there is 1 unique item, it repeats the single unique value in the output across rows. How to fix it?
VBA Code:
Public Function UDFlist( _
       ByRef x As Range) As Variant

    Dim c As New Collection

    Dim k&

    Dim y As Range

    Dim u, v

    For Each y In x
        If Application.WorksheetFunction.CountIf(x, y.Value) = 1& Then
            c.Add y.Value
        End If
    Next y

    If c.Count = 0& Then
        UDFlist = CVErr(xlErrNA)
        Exit Function
    Else
        ReDim u(1& To c.Count, 1& To 1&)
        For Each v In c
            k = (k + 1&)
            u(k, 1&) = v
        Next v
        UDFlist = u
        Exit Function
    End If

End Function

For Example, for two uniques it outputs:
1​
1​
2​
3​
3​
#N/A​
2​
#N/A​

But, for a single unique it outputs:
1​
1​
2​
1​
2​
1​
2​
1​
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How about
VBA Code:
Public Function UDFlist( _
       ByRef x As Range) As Variant


    Dim y As Range


    Dim d As Object
    Set d = CreateObject("scripting.dictionary")
    For Each y In x
      If Not d.exists(y.Value) Then
         d.Add y.Value, y.Value
      Else
         d(y.Value) = CVErr(xlErrNA)
      End If
    Next y

    UDFlist = Application.Transpose(d.Items)
End Function
 
Upvote 0
Aren't there 2 unique items in your second example?
 
Upvote 0
Nope, there are 2 distinct values, but only one is unique.
 
Upvote 0
@Fluff; It outputs results in the middle of #N/As. Can the values come at the start?

Currently,
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
2​
6​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
#N/A​
 
Upvote 0
How about
VBA Code:
Public Function UDFlist( _
       ByRef x As Range) As Variant


    Dim y As Range
    Dim i As Long, j As Long
    Dim a As Variant
    
    Dim d As Object
    Set d = CreateObject("scripting.dictionary")
   
    For Each y In x
      If Not d.exists(y.Value) Then
         d.Add y.Value, y.Value
      Else
         d(y.Value) = ""
      End If
    Next y
    ReDim a(1 To x.count, 1 To 1)
    For i = 0 To d.count - 1
      If d.Items()(i) <> "" Then
         j = j + 1
         a(j, 1) = d.Items()(i)
      End If
   Next i
   For i = j + 1 To UBound(a)
      a(i, 1) = CVErr(2042)
   Next i
    UDFlist = a
End Function
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top