Find the difference between two data sets

  • Data sets compared based on number postion\ place in first data set to position\place in second data set
  •  

    Click on below mentioned image to watch video:

    Download The Workbook

    Download The Addin

     

    Public Sh As Worksheet

    Sub DataComparision()

    'Declare a Variable for worksheet
    Set Sh = ActiveWorkbook.Sheets("FindDiff")
    Sh.Activate

    'Clear the existing Data
    Sh.Range(Cells(2, 3), Cells(Sh.UsedRange.Rows.Count, 5)).ClearContents
    Dim R As Long
    Dim Firstset As String
    Dim Secondset As String

    'Loop through all the rows
    For R = 2 To Sh.Range("A" & Rows.Count).End(xlUp).Row
    'Define the first set
    Firstset = Trim(Sh.Cells(R, 1).Value)

    'Define the second set
    Secondset = Trim(Sh.Cells(R, 2).Value)
    Sh.Cells(R, 3).Activate
    FistSetDiff = ""
    SecondSetDiff = ""

    'Created the function to retrieve difference
    Diff = FindDifference(Firstset, Secondset, FistSetDiff, SecondSetDiff)

    'Copy the first set difference in third column
    Sh.Cells(R, 3).Value = FistSetDiff

    'Copy the second set difference in fourth column
    Sh.Cells(R, 4).Value = SecondSetDiff

    'Combine these two sets - in 5th column
    If Len(Sh.Cells(R, 3).Value) > 0 And Len(Sh.Cells(R, 4).Value) > 0 Then
    Sh.Cells(R, 5).Value = FistSetDiff & "," & SecondSetDiff
    End If

    If Len(Sh.Cells(R, 3).Value) > 0 And Len(Sh.Cells(R, 4).Value) = 0 Then
    Sh.Cells(R, 5).Value = FistSetDiff
    End If

    If Len(Sh.Cells(R, 3).Value) = 0 And Len(Sh.Cells(R, 4).Value) > 0 Then
    Sh.Cells(R, 5).Value = SecondSetDiff
    End If

    'If there is no difference
    If Len(FistSetDiff) = 0 And Len(SecondSetDiff) = 0 Then
    Sh.Cells(R, 3).Value = "No difference"
    Sh.Cells(R, 4).Value = "No difference"
    Sh.Cells(R, 5).Value = "No difference"
    End If

    Cellformating (R)
    Next
    MsgBox "Hi Process Completed"
    End Sub


    Function FindDifference(Firstset, Secondset, FirstSetDiff, SecondSetDiff)
    'Loop through the first set and compare with first
    For S = 1 To Len(Firstset)
    If Mid(Firstset, S, 1) <> Mid(Secondset, S, 1) Then
    FirstSetDiff = FirstSetDiff & Mid(Firstset, S, 1) & ","
    End If
    Next
    If Len(FirstSetDiff) > 0 Then
    FirstSetDiff = Left(FirstSetDiff, Len(FirstSetDiff) - 1)
    End If

    'Loop through the second set and compare with first
    For S = 1 To Len(Secondset)
    If Mid(Secondset, S, 1) <> Mid(Firstset, S, 1) Then
    SecondSetDiff = SecondSetDiff & Mid(Secondset, S, 1) & ","
    End If
    Next
    If Len(SecondSetDiff) > 0 Then
    SecondSetDiff = Left(SecondSetDiff, Len(SecondSetDiff) - 1)
    End If
    End Function


    Function Cellformating(R)
    'Format the columns of the row
    With Sh.Range(Cells(R, 3), Cells(R, 5))
    .Font.Size = 15
    .Font.ColorIndex = 5
    .Font.Bold = True
    .HorizontalAlignment = xlLeft End With
    End Function

     

     

    Addin Code:


    Function FindDifference(FirstSet, Secondset)
    'Loop through the first set and compare with first For S = 1 To Len(FirstSet)
    If Mid(FirstSet, S, 1) <> Mid(Secondset, S, 1) Then
    FirstSetDiff = FirstSetDiff & Mid(FirstSet, S, 1) & ","
    End If
    Next
    If Len(FirstSetDiff) > 0 Then
    FirstSetDiff = Left(FirstSetDiff, Len(FirstSetDiff) - 1)
    End If

    'Loop through the second set and compare with first
    For S = 1 To Len(Secondset)
    If Mid(Secondset, S, 1) <> Mid(FirstSet, S, 1) Then
    SecondSetDiff = SecondSetDiff & Mid(Secondset, S, 1) & ","
    End If
    Next
    If Len(SecondSetDiff) > 0 Then
    SecondSetDiff = Left(SecondSetDiff, Len(SecondSetDiff) - 1)
    End If

    If Len(FirstSetDiff) > 0 Or Len(SecondSetDiff) > 0 Then
    FindDifference = FirstSetDiff & "," & SecondSetDiff
    End If

    If Len(FirstSetDiff) > 0 Or Len(SecondSetDiff) = 0 Then
    FindDifference = FirstSetDiff
    End If

    If Len(FirstSetDiff) = 0 Or Len(SecondSetDiff) > 0 Then
    FindDifference = SecondSetDiff
    End If

    If Len(FirstSetDiff) = 0 And Len(SecondSetDiff) = 0 Then
    FindDifference = "No Change"
    End If

    End Function