Greetings darkmatter,
There may be better/faster ways of doing this, but if you are looking for a basic example to see how this can be done:
In a standard module, paste:
Sub GetUnique()
Dim aFirstArray() As Variant
Dim aUniqueArray() As String
Dim lngCountFirst As Long
Dim lngCountUnique As Long
Dim bolFoundIt As Boolean
Dim strOne As String
Dim strTwo As String
' // add some values (some duplicated) to the first array//
aFirstArray() = Array("Banana", "Apple", "Orange", "Tomato", "Apple", _
"Lemon", "Lime", "Lime", "Apple")
' // Redim with one element, empty at this point.//
ReDim aUniqueArray(0)
' // loop thru ea element in our first array. (This is our outer loop)//
For lngCountFirst = LBound(aFirstArray()) To UBound(aFirstArray())
' // ensure that we flag as False at the start of ea loop//
bolFoundIt = False
' // In a secondary, inner loop, we can build the unique array, only //
' // adding items that have not already been added. //
For lngCountUnique = LBound(aUniqueArray()) To UBound(aUniqueArray())
' // For ea element in our unique array, see if it matches the //
' // current element being looked at in our frist array. If we //
' // find a match, mark our flag/boolean and exit the inner loop.//
' // On the other hand, if no match is found after every element //
' // in our unique array is looked at, then bolFoundIt will still//
' // be False. //
If aUniqueArray(lngCountUnique) = aFirstArray(lngCountFirst) Then
bolFoundIt = True
Exit For
End If
Next lngCountUnique
' // Now if bolFound is still False, then we didn't find a match, so //
' // we'll add it to the last available element in our unique array //
' // and add another empty element to the unique array for the next //
' // round... Note the use of Redim Preserve, so that we don't //
' // lose the values already added. //
If Not bolFoundIt Then
aUniqueArray(UBound(aUniqueArray())) = aFirstArray(lngCountFirst)
ReDim Preserve aUniqueArray(UBound(aUniqueArray()) + 1)
End If
Next lngCountFirst
' // Now after we're all done, we left our unique array with one //
' // extra/unused element. We'll drop/kill the extra element here. //
ReDim Preserve aUniqueArray(UBound(aUniqueArray()) - 1)
' // Just for the demo, we'll loop thru both arrays and build strings, //
' // then display the comparison in a msgbox //
For lngCountFirst = LBound(aFirstArray()) To UBound(aFirstArray())
strOne = strOne & aFirstArray(lngCountFirst) & vbCrLf
Next
For lngCountUnique = LBound(aUniqueArray()) To UBound(aUniqueArray())
strTwo = strTwo & aUniqueArray(lngCountUnique) & vbCrLf
Next
MsgBox "First array was:" & vbCrLf & strOne & String(2, vbCrLf) & _
"Second array is:" & vbCrLf & strTwo, 0, ""
End Sub
Hope this helps,
Mark