Compare two strings and highlight the different words

arvindmotha

New Member
Joined
Jun 9, 2022
Messages
6
Office Version
  1. 2007
Platform
  1. Windows
Someone please help with VBA code to find difference between 2 strings and highlight the exact words in a separate cell along with the original text. Should work well even for relatively large text fields like 500-1000 words.

A1 = The quick brown fox jumps over the lazy dog
B1 =The quick brown fox over the lazy dog

C1 should return = The quick brown fox jumps over the lazy dog

Thanks in advance!
 
Can you give us the two sample texts in a form that we can copy to test? We cannot copy from an image.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Thank you sharing. This worked perfectly well for small texts as shown in your results. But larger text field matches had some anomalies. Even words with no differences were getting highlighted
View attachment 66782

Your posts are mixed up indeed. The above image shows the (poor) result with my code. Below is the result with @Peter_SSs 's code:
1654949316001.png

Also not exactly what we're after, but his approach using Regex is much more promising than mine.

Here is the original text of A1:
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
And here with missing words, in B1:
Lorem Ipsum is simply text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
 
Upvote 0
This worked perfectly well for small texts as shown in your results. But larger text field matches had some anomalies.
It is not so much small and large but your original one sample had no punctuation and no digits. The latest text has both.

Updated code:
VBA Code:
Sub DifferentWords_v2()
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  Application.ScreenUpdating = False
  With Range("A2", Range("B" & Rows.Count).End(xlUp))
    a = .Value
    .Font.Color = vbRed
    For i = 1 To UBound(a)
      RX.Pattern = "(^| )(" & Replace(Replace(a(i, 1), " ", "|"), ".", "\.") & ")(?= |$)"
      For Each M In RX.Execute(a(i, 2))
        .Cells(i, 2).Characters(M.FirstIndex + 1, M.Length).Font.Color = 0
      Next M
      RX.Pattern = "(^| )(" & Replace(Replace(a(i, 2), " ", "|"), ".", "\.") & ")(?= |$)"
      For Each M In RX.Execute(a(i, 1))
        .Cells(i, 1).Characters(M.FirstIndex + 1, M.Length).Font.Color = 0
      Next M
    Next i
  End With
  Application.ScreenUpdating = True
End Sub

Result based on text provided in post 12:

1654952788523.png
 
Upvote 0
Great result, neat solution! I do wonder though why it doesn't find the missing "dummy" on the first line.
 
Upvote 0
I do wonder though why it doesn't find the missing "dummy" on the first line.
Because dummy is found in both cells.
The code is highlighting words that are in one cell but not in the other.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Compare two strings and highlight the different words
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top