Skip to content

Instantly share code, notes, and snippets.

@witwall witwall/count.vba
Last active Feb 26, 2016

What would you like to do?
Function ArrayUnique(ByVal aArrayIn As Variant) As Variant
' ArrayUnique
' This function removes duplicated values from a single dimension array
Dim aArrayOut() As Variant
Dim bFlag As Boolean
Dim vIn As Variant
Dim vOut As Variant
Dim i%, j%, k%
ReDim aArrayOut(LBound(aArrayIn) To UBound(aArrayIn))
i = LBound(aArrayIn)
j = i
For Each vIn In aArrayIn
For k = j To i - 1
If vIn = aArrayOut(k) Then bFlag = True: Exit For
If Not bFlag Then aArrayOut(i) = vIn: i = i + 1
bFlag = False
If i <> UBound(aArrayIn) Then ReDim Preserve aArrayOut(LBound(aArrayIn) To i - 1)
ArrayUnique = aArrayOut
End Function
Public Function Distinct(ByVal str As String, Optional delimiter As String = ",") As String
Dim aReturn As Variant
Dim aArray As Variant
aArray = Split(str, delimiter) ' Array(1, 2, 3, 1, 2, 3, "Test", "Test")
aReturn = ArrayUnique(aArray)
Distinct = Join(aReturn, delimiter)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.