Home » excel » Comparing two text cells and show the differnce in third column in Excel sheet 2010/2007

Comparing two text cells and show the differnce in third column in Excel sheet 2010/2007

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to compare two cells which contains text and show the difference in some colour

Ex:Cell A:NTR is a great Leader.
   Cell B:Ntr is a Great leader
How to&Answers:

Using VBA…..

Sub CompareInColor()

ActiveSheet.Range("C1").Value = ActiveSheet.Range("A1").Value

For i = 1 To Len(ActiveSheet.Range("A1").Value)
    If (ActiveSheet.Range("A1").Characters(i, 1).Text <> 
        ActiveSheet.Range("B1").Characters(i, 1).Text) Then 
        ActiveSheet.Range("C1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
Next i

End Sub

This compares A1 and B1 only… Loop through your rows if you have many of them. Also, I assumed that the length of A1 and A2 is the same, otherwise an out-of-range index error may evolve.

Answer:

Sub CompareInColor()

ActiveSheet.Range("C1").Value = ActiveSheet.Range("A1").Value

For i = 1 To Len(ActiveSheet.Range("A1").Value)
    If (ActiveSheet.Range("A1").Characters(i, 1).Text <> ActiveSheet.Range("B1").Characters(i, 1).Text) Then
        ActiveSheet.Range("C1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
End If
Next i

End Sub

Sub CompareInColorFlip()

ActiveSheet.Range("D1").Value = ActiveSheet.Range("B1").Value

For i = 1 To Len(ActiveSheet.Range("B1").Value)
    If (ActiveSheet.Range("B1").Characters(i, 1).Text <> ActiveSheet.Range("A1").Characters(i, 1).Text) Then
        ActiveSheet.Range("D1").Characters(i, 1).Font.Color = RGB(255, 0, 0)
End If
Next i

End Sub

This Compares A to B then changes font for differences in C and B to A then changes font for differences in D