Search code examples
excelvbaduplicateschain

Isolate a non duplicate of a chain separated by "," from another longer chain in VBA


For example if in the celle A1 you have: 1, 2, 3, 4 ,5 ,6 ,7 ,8 ,9 And in A2 you have: 1, 3, 6 ,9 , 15 I would like to find a VBA function F which will do that: F(A1; A2)= 15, what it means that it will give the only character which is not in the chain of data seen in A1 And if we have in B1: 1, 2 ,5 ,8, 15, 20. F will do that: F(A1; B2)=15, 20 (separated by "," or something else), which are the only 2 characters which don't appear in my cell A1.Etc and etc, if you have 2, 3 or 4 characters which don't appear in the data chain A1. Someone could give me such a code please ?

Thanks.


Solution

  • 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