This function will work for you
Function ReturnUnique(cell1 As Range, cell2 As Range) As String
ReturnUnique = ""
Dim v1 As Variant, v2 As Variant
v1 = Split(cell1.Value, ",")
v2 = Split(cell2.Value, ",")
Dim i As Long, j As Long
Dim bool As Boolean
For i = LBound(v1, 1) To UBound(v1, 1)
bool = True
For j = LBound(v2, 1) To UBound(v2, 1)
If v2(j) = v1(i) Then
bool = False
Exit For
End If
Next j
If bool Then
If ReturnUnique = "" Then
ReturnUnique = v1(i)
Else
ReturnUnique = ReturnUnique & ", " & v1(i)
End If
End If
Next i
For i = LBound(v2, 1) To UBound(v2, 1)
bool = True
For j = LBound(v1, 1) To UBound(v1, 1)
If v1(j) = v2(i) Then
bool = False
Exit For
End If
Next j
If bool Then
If ReturnUnique = "" Then
ReturnUnique = v2(i)
Else
ReturnUnique = ReturnUnique & ", " & v2(i)
End If
End If
Next i
End Function
EDIT:
Try this function instead
Function ReturnUnique(cell1 As Range, cell2 As Range) As String
ReturnUnique = ""
Dim v1 As Variant, v2 As Variant
v1 = Split(Trim(cell1.Value), ",")
v2 = Split(Trim(cell2.Value), ",")
Dim i As Long, j As Long
Dim bool As Boolean
For i = LBound(v2, 1) To UBound(v2, 1)
bool = True
For j = LBound(v1, 1) To UBound(v1, 1)
If Trim(v1(j)) = Trim(v2(i)) Then
bool = False
Exit For
End If
Next j
If bool Then
If ReturnUnique = "" Then
ReturnUnique = v2(i)
Else
ReturnUnique = ReturnUnique & ", " & v2(i)
End If
End If
Next i
End Function
4
solved Isolate a non duplicate of a chain separated by “,” from another longer chain in VBA [closed]