Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- 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?
For Example, for two uniques it outputs:
But, for a single unique it outputs:
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 |