Public Function fnDiff(s1 As Variant, s2 As Variant) As String
Dim strSource As String
Dim strTarget As String
Dim intOuterLoop As Integer
Dim intInnerLoop As Integer
Dim arrSource() As String
Dim arrTarget() As String
Dim intLowerBoundSource As Integer
Dim intUpperBoundSource As Integer
Dim intLowerBoundTarget As Integer
Dim intUpperBoundTarget As Integer
Dim intLowerBoundResult As Integer
Dim intUpperBoundResult As Integer
Dim strResult As String
Dim bolSwap As Boolean
Dim i As Integer
Dim bolOKToAdd As Boolean
Dim strTemp As String
s1 = s1 & ""
s2 = s2 & ""
If CountOccurrence(s1, ",") > CountOccurrence(s2, ",") Then
bolSwap = True
strSource = s2
strTarget = s1
Else
strSource = s1
strTarget = s2
End If
If strSource = vbNullString Then
If InStrRev(strTarget, ",") = Len(strTarget) And strTarget <> "" Then strTarget = left(strTarget, Len(strTarget) - 1)
If InStr(strTarget, ",") = 1 Then strTarget = Mid(strTarget, 2)
strResult = strTarget
fnDiff = strTarget
Exit Function
End If
If InStr(strSource, ",") = 0 Then
ReDim arrSource(0)
arrSource(0) = strSource
Else
arrSource = Split(strSource, ",")
End If
arrTarget = Split(strTarget, ",")
intLowerBoundSource = LBound(arrSource)
intUpperBoundSource = UBound(arrSource)
intLowerBoundTarget = LBound(arrTarget)
intUpperBoundTarget = UBound(arrTarget)
ReDim arrResult(0)
'trim spaces
For intOuterLoop = intLowerBoundSource To intUpperBoundSource
arrSource(intOuterLoop) = RTrim(LTrim(arrSource(intOuterLoop)))
Next
For intOuterLoop = intLowerBoundTarget To intUpperBoundTarget
arrTarget(intOuterLoop) = RTrim(LTrim(arrTarget(intOuterLoop)))
Next
'check for duplicate
For intOuterLoop = intLowerBoundSource To intUpperBoundSource
For intInnerLoop = intLowerBoundTarget To intUpperBoundTarget
If arrSource(intOuterLoop) = arrTarget(intInnerLoop) Then
strTemp = strTemp & arrSource(intOuterLoop) & "|"
arrSource(intOuterLoop) = vbNullString
arrTarget(intInnerLoop) = vbNullString
Else
If InStr(strTemp, arrTarget(intInnerLoop)) > 0 Then arrTarget(intInnerLoop) = vbNullString
End If
Next intInnerLoop
Next intOuterLoop
For intOuterLoop = intLowerBoundTarget To intUpperBoundTarget
For intInnerLoop = intLowerBoundSource To intUpperBoundSource
If arrTarget(intOuterLoop) = arrSource(intInnerLoop) Then
strTemp = strTemp & arrTarget(intOuterLoop) & "|"
arrTarget(intOuterLoop) = vbNullString
arrSource(intInnerLoop) = vbNullString
Else
If InStr(strTemp, arrSource(intInnerLoop)) > 0 Then arrSource(intInnerLoop) = vbNullString
End If
Next intInnerLoop
Next
'recheck duplicate, just make sure
For intOuterLoop = intLowerBoundSource To intUpperBoundSource
For intInnerLoop = intLowerBoundSource To intUpperBoundSource
If intInnerLoop <> intOuterLoop Then
If arrSource(intOuterLoop) = arrSource(intInnerLoop) Then arrSource(intInnerLoop) = vbNullString
End If
Next
Next
For intOuterLoop = intLowerBoundTarget To intUpperBoundTarget
For intInnerLoop = intLowerBoundTarget To intUpperBoundTarget
If intInnerLoop <> intOuterLoop Then
If arrTarget(intOuterLoop) = arrTarget(intInnerLoop) Then arrTarget(intInnerLoop) = vbNullString
End If
Next
Next
If bolSwap Then
For intOuterLoop = intLowerBoundTarget To intUpperBoundTarget
If arrTarget(intOuterLoop) <> vbNullString Then strResult = strResult & arrTarget(intOuterLoop) & ", "
Next
For intOuterLoop = intLowerBoundSource To intUpperBoundSource
If arrSource(intOuterLoop) <> vbNullString Then strResult = strResult & arrSource(intOuterLoop) & ", "
Next
Else
For intOuterLoop = intLowerBoundSource To intUpperBoundSource
If arrSource(intOuterLoop) <> vbNullString Then strResult = strResult & arrSource(intOuterLoop) & ", "
Next
For intOuterLoop = intLowerBoundTarget To intUpperBoundTarget
If arrTarget(intOuterLoop) <> vbNullString Then strResult = strResult & arrTarget(intOuterLoop) & ", "
Next
End If
If Len(strResult) > 0 Then strResult = left(strResult, Len(strResult) - 2)
fnDiff = strResult
End Function
Public Function CountOccurrence(ByVal strString As String, ByVal strStringToCount As String) As Integer
Dim intCounter As Integer
Dim intLoop As Integer
Dim intStringLen As Integer
intStringLen = Len(strString)
For intLoop = 1 To intStringLen
If Mid(strString, intLoop, 1) = strStringToCount Then intCounter = intCounter + 1
Next
''intLoop = InStr(1, strString, strStringToCount)
'While intLoop > 0
' intCounter = intCounter + 1
' intLoop = InStr(intLoop + 1, strString, strStringToCount)
'Wend
CountOccurrence = intCounter
End Function