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
LVL 11
bsharathAsked:
Who is Participating?
 
TracyConnect With a Mentor VBA DeveloperCommented:
Attached is the amended version which pulls the differences out of both D and E.
Diff-v2.xls
0
 
nutschCommented:
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

0
 
bsharathAuthor Commented:
I will have 1 to 100's of email id's that will not match
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
nutschCommented:
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

0
 
bsharathAuthor Commented:
How should i run this. Is it a macro?
0
 
nutschCommented:
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

0
 
bsharathAuthor Commented:
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
0
 
TracyVBA DeveloperCommented:
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
0
 
bsharathAuthor Commented:
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
0
 
nutschConnect With a Mentor Commented:
Here is an update that will take care of blank cells.



Function diffMail(str1 As String, str2 As String)
if str1="" then 
diffmail=str2
exit function
end if

if str2="" then 
diffmail=str1
exit function
end if


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

0
 
bsharathAuthor Commented:
Thank you
Can you help with this related post
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_25976948.html
need to get the difference in 2 different colums so i know the difference is from which colum
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.