Compare word by word between two cells and highlight difference

Jmac2604

New Member
Joined
Jun 11, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a macro to compare words between cells from column B and D. Currently it is highlighting the matching words in column D. instead I want to highlight the column B. pasted the code below. Can someone pls help me on this?


Sub CompareWords()
Dim xStr() As String
Dim i As Long
Dim x As Long, y As Long

With ActiveSheet
For i = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
xStr = Split(.Cells(i, "B").Value, " ")
With .Cells(i, "D")
.Font.ColorIndex = 1
For x = LBound(xStr()) To UBound(xStr())
For y = 1 To Len(.Text)
If Mid(.Text, y, Len(xStr(x))) = xStr(x) Then

.Characters(y, Len(xStr(x))).Font.ColorIndex = 4

End If
Next y
Next x
End With
Next i
End With
MsgBox "completed"
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This should work! I have just swapped the B and D references
VBA Code:
Sub CompareWords()
Dim xStr() As String
Dim i As Long
Dim x As Long, y As Long

With ActiveSheet
For i = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
xStr = Split(.Cells(i, "D").Value, " ")
With .Cells(i, "B")
.Font.ColorIndex = 1
For x = LBound(xStr()) To UBound(xStr())
For y = 1 To Len(.Text)
If Mid(.Text, y, Len(xStr(x))) = xStr(x) Then

.Characters(y, Len(xStr(x))).Font.ColorIndex = 4

End If
Next y
Next x
End With
Next i
End With
MsgBox "completed"
End Sub
 
Upvote 0
Thanks Johnmpc for your response. I tried this before but actually after swapping the code highlights all the words even though its different. So is it a way to not swap the column and highlight column B (source - words to be checked) and not column D.
Thanks again for your support.
 
Upvote 0
Thanks Johnmpc for your response. I tried this before but actually after swapping the code highlights all the words even though its different. So is it a way to not swap the column and highlight column B (source - words to be checked) and not column D.
Thanks again for your support.
Did you copy and paste my code in?
I didn't swap the First reference to column B.
 
Upvote 0
Did you copy and paste my code in?
I didn't swap the First reference to column B.
Yes I pasted your code and tried. Actually the split should happen in the B column only and it should check the D column and highlight the matching in the B column. Sorry If am confusing you.
 
Upvote 0
oh, i understand, so you have multiple words in each cell and only want to highlight the matching word in column B.
Does the code work the other way around?
 
Upvote 0
RankTitleGenreWorldwide grossYear
Users/Healthcare professionals should be familiar with surgical procedures and techniques involving non-absorbable sutures before employing Suture for wound closure, as risk of wound dehiscence (In MDR IFU: as risk of tissue separation/wound dehiscence leading to impaired healing )may vary with the site of application and the suture material usedUsers should be familiar with surgical procedures and techniques involving absorbable sutures before employing for wound closure, as the risk of wound dehiscence may vary with the site of application and the suture material used. Surgeons should consider the in vivo performance (under PERFORMANCE section) when selecting a suture.
 
Upvote 0
Bit more complicated once you talk about long sentences. Because words like "a" and "in" are within other words.

See if this is close enough.

VBA Code:
Sub CompareWords()
Dim xStr() As String
Dim i As Long
Dim x As Long, y As Long

With ActiveSheet
For i = 2 To .Cells(.Rows.Count, "D").End(xlUp).Row
xStr = Split(.Cells(i, "D").Value, " ")
With .Cells(i, "B")
.Font.ColorIndex = 1
For x = LBound(xStr()) To UBound(xStr())
For y = 1 To Len(.Text)
If Mid(.Text, y, Len(xStr(x))) = xStr(x) Then

.Characters(y, Len(xStr(x))).Font.ColorIndex = 4

End If
Next y
Next x
End With
Next i
End With
MsgBox "completed"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,422
Messages
6,124,808
Members
449,191
Latest member
rscraig11

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