Find the difference between two data sets
Public Sh As Worksheet
Sub DataComparision()
Set Sh = ActiveWorkbook.Sheets("FindDiff")
Sh.Activate
Sh.Range(Cells(2, 3), Cells(Sh.UsedRange.Rows.Count, 5)).ClearContents
Dim R As Long
Dim Firstset As String
Dim Secondset As String
For R = 2 To Sh.Range("A" & Rows.Count).End(xlUp).Row
Firstset = Trim(Sh.Cells(R, 1).Value)
Secondset = Trim(Sh.Cells(R, 2).Value)
Sh.Cells(R, 3).Activate
FistSetDiff = ""
SecondSetDiff = ""
Diff = FindDifference(Firstset, Secondset, FistSetDiff, SecondSetDiff)
Sh.Cells(R, 3).Value = FistSetDiff
Sh.Cells(R, 4).Value = SecondSetDiff
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 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)
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
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)
With Sh.Range(Cells(R, 3), Cells(R, 5))
.Font.Size = 15
.Font.ColorIndex = 5
.Font.Bold = True
.HorizontalAlignment = xlLeft
End With
End Function
Function FindDifference(FirstSet, Secondset)
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
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