Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Formula or script that can find difference between 2 colums . the data between each coma has to be queried.

Hi,

Formula or script that can find difference between 2 colums . the data between each coma has to be queried.
Attached a sample. I want to check whats the email thats single and does not have a match in the other colum.

Regards
sharath

Diff.xls
Avatar of nutsch
nutsch
Flag of United States of America image

Hi Sharath,

You can use the following udf, assuming that there is only one single non-matching e-mail in your list.

Thomas

Function diffMail(str1 As String, str2 As String)
Dim arr1, arr2
Dim i As Long, j As Long
arr1 = Split(str1, ",")
arr2 = Split(str2, ",")

For i = 0 To UBound(arr1)
    For j = 0 To UBound(arr2)
        If arr2(j) = arr1(i) Then GoTo nxti
    Next j
    diffMail = arr1(i)
    Exit Function
nxti:
Next i

For i = 0 To UBound(arr2)
    For j = 0 To UBound(arr1)
        If arr1(j) = arr2(i) Then GoTo nxti2
    Next j
    diffMail = arr2(i)
    Exit Function
nxti2:
Next i

End Function

Open in new window

Avatar of bsharath

ASKER

I will have 1 to 100's of email id's that will not match
This update should take everything you throw at it


Function diffMail(str1 As String, str2 As String)
Dim arr1, arr2
Dim i As Long, j As Long
arr1 = Split(str1, ",")
arr2 = Split(str2, ",")

For i = 0 To UBound(arr1)
    For j = 0 To UBound(arr2)
        If arr2(j) = arr1(i) Then GoTo nxti
    Next j
    diffMail = diffMail & "," & arr1(i)
nxti:
Next i

For i = 0 To UBound(arr2)
    For j = 0 To UBound(arr1)
        If arr1(j) = arr2(i) Then GoTo nxti2
    Next j
    diffMail = diffMail & "," & arr2(i)
nxti2:
Next i

diffMail = Right(diffMail, Len(diffMail) - 1)

End Function

Open in new window

How should i run this. Is it a macro?
It's a user defined function. You copy it in a module just like a macro and you call it from the worksheet. In your case, in cell J2, you'd use the following formula, then copy down.

 =diffMail(D2,E2)

Warning. If you put the code in a personal macro workbook, you'll have to call the function with the name of that workbook, as in
 =personal.xls!diffMail(D2,E2)

Thomas

thanks its perfetc
Some cases both or any 1 will have empty cells in the colum. Can i not get
#VALUE!
Can i get them blank if both nil or 1 cell is nil
Avatar of Tracy
You can also try this.  It's a macro called FindDifferences.  It will find the differences for as many rows as you have and it will enter the difference in column J.  
Sub FindDifferences()

    Dim myValue1 As String 'email1 Value from Column D
    Dim myValue2 As String 'email1 Value from Column E
    Dim emailArray1 As Variant 'Holds email1s from myValue1 (Column D)
    Dim emailArray2 As Variant 'Holds email1s from myValue2 (Column E)
    Dim blnMatch As Boolean
    Dim lastRow As Long
    Dim i As Long
    Dim p As Long
    Dim k As Long
    
    
    lastRow = Range("D" & Rows.Count).End(xlUp).Row
    For k = 2 To lastRow
    
        blnMatch = False
        Cells(k, 10).Value = "" 'Clear out difference
    
        myValue1 = Cells(k, "D").Value
        emailArray1 = Split(myValue1, ",")
        For i = 0 To UBound(emailArray1)
           Debug.Print emailArray1(i)
        Next i
    
        myValue2 = Cells(k, "E").Value
        emailArray2 = Split(myValue2, ",")
        For i = 0 To UBound(emailArray2)
           Debug.Print emailArray2(i)
        Next i
    
        For i = 0 To UBound(emailArray1)
            For p = 0 To UBound(emailArray2)
                If emailArray1(i) = emailArray2(p) Then
                    'Match
                    blnMatch = True
                    Exit For
                Else
                End If
            Next p
    
            If blnMatch = False Then
                'Add to Diff in Column J
                If Len(Cells(k, 10).Value) > 1 Then
                    'Add to existing values
                    Cells(k, 10).Value = Cells(k, 10).Value & ", " & emailArray1(i)
                Else
                    'First value added
                    Cells(k, 10).Value = emailArray1(i)
                End If
            Else
                'Reset bool to false
                blnMatch = False
            End If
            
        Next i
        
        If Len(Cells(k, 10).Value) = 0 Then
            Cells(k, 10).Value = "No Difference"
        Else
        End If
        
    Next k

End Sub

Open in new window

Diff-1-.xls
broomee9
thanks Script works but it gets just the D drive different with E into J and not E's difference. Can i get the E mismatch to K
ASKER CERTIFIED SOLUTION
Avatar of Tracy
Tracy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you
Can you help with this related post
https://www.experts-exchange.com/questions/25976948/Script-that-finds-difference-Can-i-have-a-way-to-get-each-colum-Difference-in-colum-H-I-or-J-K.html
need to get the difference in 2 different colums so i know the difference is from which colum