Hi,
I am trying to compare the text content of two cells.
Basically, there is a review of the text in 100+ cells in column M, which is introduced in column O.
I would like the macro to highlight the differences for cells in those two columns (i.e. M1 to O1, M2 to O2, etc).
I would like to highlight the differences in red and bold, without changing the case or any other properties.
i.e.
M1 = Text in column M.
O1 = Reviewed text in Column O
"Reviewed" should be highlighted, as well as "M", "O" and "."
It should be case sensitive as well.
My knowledge is very limited when it comes to VBA.
I have been doing some research around and I found the code below, which partially works, but not completely.
Can someone help me with this?
Many thanks,
Luis
I am trying to compare the text content of two cells.
Basically, there is a review of the text in 100+ cells in column M, which is introduced in column O.
I would like the macro to highlight the differences for cells in those two columns (i.e. M1 to O1, M2 to O2, etc).
I would like to highlight the differences in red and bold, without changing the case or any other properties.
i.e.
M1 = Text in column M.
O1 = Reviewed text in Column O
"Reviewed" should be highlighted, as well as "M", "O" and "."
It should be case sensitive as well.
My knowledge is very limited when it comes to VBA.
I have been doing some research around and I found the code below, which partially works, but not completely.
Code:
Sub Compare()
If StringCompareHighlight(Range("m1"), Range("o1")) Then
Range("q1").Value = "Match"
Else
Range("q1").Value = "Not Match"
End If
If StringCompareHighlight(Range("m2"), Range("o2")) Then
Range("q2").Value = "Match"
Else
Range("q2").Value = "Not Match"
End If
If StringCompareHighlight(Range("m3"), Range("o3")) Then
Range("q3").Value = "Match"
Else
Range("q3").Value = "Not Match"
End If
End Sub
Function StringCompareHighlight(r1 As Range, r2 As Range) As Boolean
''this function compare the words from 2 strings
''each word is separated by "," and the order of these words does not matter
''return true if matches, False if not match
''changes the format of a word in one string that does not exist in the other string
Dim oMatches As Object, oMatch As Object
Dim r(1 To 2) As Range
Dim i As Integer, bDiff As Boolean, iStart As Integer
Set r(1) = r1
Set r(2) = r2
With CreateObject("vbscript.regexp")
.Pattern = " *(\w+) *(?= .*\|)(?!.*\|.* *\1 *)"
.Global = True
.IgnoreCase = True
For i = 1 To 2
Set oMatches = .Execute(" " & r(i).Text & ",|," & r(3 - i).Text & " ")
For Each oMatch In oMatches
iStart = InStr(oMatch.FirstIndex + 1, r(i).Text, oMatch.submatches(0), vbTextCompare)
With r(i).Characters(Start:=iStart, Length:=Len(oMatch.submatches(0))).Font
.Bold = True
.Size = 14
End With
Next oMatch
If oMatches.Count > 0 Then bDiff = True
Next i
End With
StringCompareHighlight = Not bDiff
End Function
Can someone help me with this?
Many thanks,
Luis