VBA code for Text Diff between two columns?

Akila868

New Member
Joined
Aug 28, 2019
Messages
8
Hi friends,

I need big help on the following.

I have two columns with texts. Column A has the old text and Column B has the new text. I need to match these two and show in a 3rd column the additional characters (highlighted in red color) in B column compared to A column text. Further in D column to show the additional characters separately.

Would really appreciate if this can be done. Thanks in advance. :)
Ex.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Column A
[/TD]
[TD]Column B
[/TD]
[TD]Column C
[/TD]
[TD]Column D
[/TD]
[/TR]
[TR]
[TD]Test123$ New.
[/TD]
[TD]Test123E Newest
[/TD]
[TD]Test123E Newest
[/TD]
[TD]Eest
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try this :
Code:
Private Sub CompareAndWriteWithColor()
  Dim TxtA As String
  Dim TxtB As String
  Dim TxtC As String
  Dim TxtD As String
  Dim intPos As Integer
  Dim strColorPosition() As String
  Dim blDimensioned As Boolean
  Dim strText As String
  Dim lngPosition As Long
  blDimensioned = False
  Sheets("Sheet3").Select
  TxtA = Range("a2").Value
  TxtB = Range("b2").Value
  For intPos = 1 To Len(TxtB)
   If Mid(TxtB, intPos, 1) = Mid(TxtA, intPos, 1) Then
    TxtC = TxtC & Mid(TxtB, intPos, 1)
   Else
    TxtC = TxtC & Mid(TxtB, intPos, 1)
    TxtD = TxtD & Mid(TxtB, intPos, 1)
    Do
     strText = intPos
     If strText <> "" Then
      If blDimensioned = True Then
       ReDim Preserve strColorPosition(0 To UBound(strColorPosition) + 1) As String
      Else
       ReDim strColorPosition(0 To 0) As String
       blDimensioned = True
      End If
      strColorPosition(UBound(strColorPosition)) = strText
      Exit Do
     End If
    Loop Until strText = ""
   End If
  Next
  Range("c2").Value = TxtC
  For lngPosition = LBound(strColorPosition) To UBound(strColorPosition)
   ActiveCell.Characters(Start:=strColorPosition(lngPosition), Length:=1).Font.ColorIndex = 3
  Next lngPosition
  Range("d2").Value = TxtD
End Sub
 
Upvote 0
Try this :
Code:
Private Sub CompareAndWriteWithColor()
  Dim TxtA As String
  Dim TxtB As String
  Dim TxtC As String
  Dim TxtD As String
  Dim intPos As Integer
  Dim strColorPosition() As String
  Dim blDimensioned As Boolean
  Dim strText As String
  Dim lngPosition As Long
  blDimensioned = False
  Sheets("Sheet3").Select
  TxtA = Range("a2").Value
  TxtB = Range("b2").Value
  For intPos = 1 To Len(TxtB)
   If Mid(TxtB, intPos, 1) = Mid(TxtA, intPos, 1) Then
    TxtC = TxtC & Mid(TxtB, intPos, 1)
   Else
    TxtC = TxtC & Mid(TxtB, intPos, 1)
    TxtD = TxtD & Mid(TxtB, intPos, 1)
    Do
     strText = intPos
     If strText <> "" Then
      If blDimensioned = True Then
       ReDim Preserve strColorPosition(0 To UBound(strColorPosition) + 1) As String
      Else
       ReDim strColorPosition(0 To 0) As String
       blDimensioned = True
      End If
      strColorPosition(UBound(strColorPosition)) = strText
      Exit Do
     End If
    Loop Until strText = ""
   End If
  Next
  Range("c2").Value = TxtC
  For lngPosition = LBound(strColorPosition) To UBound(strColorPosition)
   ActiveCell.Characters(Start:=strColorPosition(lngPosition), Length:=1).Font.ColorIndex = 3
  Next lngPosition
  Range("d2").Value = TxtD
End Sub

Hi, thank you so much for the code. This actually highlights the words with differences. Is it possible to make it to highlight the additional or missing character/s only. It could be a letter, number or a special character.
 
Upvote 0
In your example '$' is replaced with 'E".
Do you mean to say that you want only '$' and est' omitting the 'E' which replaced '$'?
Give an example of how the result should be.
 
Last edited:
Upvote 0
No what I meant is Column C shows the full word highlighted, if there was a change in that specific word. It should instead highlight the characters that came in new/ removed/ replaced by comparing the two words in two columns. Sorry if the requirement was not clear.

Example

[TABLE="width: 500"]
<tbody>[TR]
[TD]Column A
[/TD]
[TD]Column B
[/TD]
[TD]Column C
[/TD]
[TD]Column D
[/TD]
[/TR]
[TR]
[TD]ABC$123.
[/TD]
[TD]ABCU123.
[/TD]
[TD]ABCU123.
[/TD]
[TD]U
[/TD]
[/TR]
[TR]
[TD]DDD;889@
[/TD]
[TD]DDDL889P
[/TD]
[TD]DDDL889p
[/TD]
[TD]Lp
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Just need to compare character by character from left to right in the same order and display difference in characters. Hope this is clear now. Thank you so much for taking the time to help me. Really appreciate it.
 
Upvote 0
Hi, Akila868.
Try this:
Since the code reads each character one by one then it will be slow if you have large data.
Code:
[FONT=lucida console][color=Royalblue]Sub[/color] compareChar2()
[i][color=seagreen]'https://www.mrexcel.com/forum/excel-questions/1108363-vba-code-text-diff-between-two-columns.html[/color][/i]
[color=Royalblue]Dim[/color] i [color=Royalblue]As[/color] [color=Royalblue]Long[/color], j [color=Royalblue]As[/color] [color=Royalblue]Long[/color], n [color=Royalblue]As[/color] [color=Royalblue]Long[/color], k [color=Royalblue]As[/color] [color=Royalblue]Long[/color], y [color=Royalblue]As[/color] [color=Royalblue]Long[/color]
[color=Royalblue]Dim[/color] va

    Application.ScreenUpdating = [color=Royalblue]False[/color]

n = Range([color=brown]"A"[/color] & Rows.count).[color=Royalblue]End[/color](xlUp).Row
va = Range([color=brown]"A1:B"[/color] & n)
Range([color=brown]"C2:C"[/color] & n).Value = Range([color=brown]"B2:B"[/color] & n).Value

    [color=Royalblue]For[/color] i = [color=crimson]2[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color])
        j = Len(va(i, [color=crimson]1[/color])): y = Len(va(i, [color=crimson]2[/color]))
        [color=Royalblue]If[/color] y > j [color=Royalblue]Then[/color] va(i, [color=crimson]1[/color]) = va(i, [color=crimson]1[/color]) & WorksheetFunction.Rept([color=brown]" "[/color], y - j)
        tx = [color=brown]""[/color]
        [color=Royalblue]For[/color] k = [color=crimson]1[/color] [color=Royalblue]To[/color] y
          [color=Royalblue]If[/color] [color=Royalblue]Mid[/color](va(i, [color=crimson]1[/color]), k, [color=crimson]1[/color]) <> [color=Royalblue]Mid[/color](va(i, [color=crimson]2[/color]), k, [color=crimson]1[/color]) [color=Royalblue]Then[/color]
              [color=Royalblue]With[/color] Cells(i, [color=crimson]3[/color]).Characters(k, [color=crimson]1[/color])
              .Font.Color = vbRed
              tx = tx & .[color=Royalblue]Text[/color]
              [color=Royalblue]End[/color] [color=Royalblue]With[/color]
          [color=Royalblue]End[/color] [color=Royalblue]If[/color]
        [color=Royalblue]Next[/color] k
        Cells(i, [color=crimson]4[/color]) = tx
    [color=Royalblue]Next[/color] i
    
    Application.ScreenUpdating = [color=Royalblue]True[/color]
[color=Royalblue]End[/color] [color=Royalblue]Sub[/color][/FONT]
 
Upvote 0
Thanks @ Akuini. Really appreciate it. Still the code highlights the full word instead of the character that is newly added.
 
Upvote 0
Thanks @ Akuini. Really appreciate it. Still the code highlights the full word instead of the character that is newly added.

Here's the result I got, it isn't what you expected? [TABLE="width: 426"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ABC$123.[/TD]
[TD]ABCU123.[/TD]
[TD]ABCU123.
[/TD]
[TD]U[/TD]
[/TR]
[TR]
[TD]DDD;889@[/TD]
[TD]DDDL889P[/TD]
[TD]DDDL889P
[/TD]
[TD]LP[/TD]
[/TR]
[TR]
[TD]Dustin[/TD]
[TD]Duxtinx[/TD]
[TD]Duxtinx
[/TD]
[TD]xx[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,147
Members
452,615
Latest member
bogeys2birdies

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