Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- Windows
I want to list only the non-repeating items from a range. Essentially I want to output only non-duplicated items. For example, if range A1:A8 has A, B, C, D, E, F, A, B then I want only C, D, E, F as output. The function I have put together gives all distinct as of now, that is, A, B, C, D, E, F. But, I want it to output C, D, E, F only.
VBA Code:
Option Explicit
Option Base 1
Public Function PrintNR(ByRef x As Variant, Optional ByVal y As Boolean) As Variant
' If y = 1, then the function returns a variant count of distinct elements (TRUE).
' If y = 0, then the function returns a variant array of distinct elements (FALSE).
' It must be array-entered into a range of cells (Ctrl+Shift+Enter).
Dim Bln As Boolean
Dim i As Long
Dim j As Long
Dim w As Variant
Dim u() As Variant
If IsMissing(y) Then y = True
i = 0
j = 0
For Each w In x
Bln = False
For i = 1 To j
If w = u(i) Then
Bln = True
Exit For
End If
Next i
If Not Bln And Not IsEmpty(w) Then
j = j + 1
ReDim Preserve u(j)
u(j) = w
End If
Next w
If y Then
PrintNR = CLng(j)
Else
PrintNR = CVar(u)
End If
End Function