我正在尝试将A列中的每个单词与B列中的每个单词进行匹配。换句话说,我要检查B列中是否存在A列的单词。如果是,则突出显示B列中相同。
我尝试了下面的代码,但这看起来完全匹配。
Dim xStr As String
Dim i, Y As Variant
Dim M, j As Long
count = Range("A4", Range("A4").End(xlDown)).Rows.count
For i = 4 To count + 3
xStr = Range("B" & i).value
With Range("G" & i)
.Font.ColorIndex = 1
For j = 1 To Len(.Text)
If Mid(.Text, j, Len(xStr)) = xStr Then .Characters(j, Len(M)).Font.ColorIndex = 3
Next j
End With
Next i
示例:
A#列的BAND AID WASHPROOF
B ##列强生(Johnson&Johnson)耐洗防腐创可贴(Jar)
A列中的3个单词应在B列中突出显示。
答案 0 :(得分:0)
我假设您要比较B4和G4中的单词,然后比较B5和G5中的单词,依此类推-每对分别比较。
如果您用空格作为定界符Split
{\ 1}的内容,那么您的单词在数组中,并且可以比较每个数组元素。
Private Sub CompareWords()
Dim xStr() As String
Dim i As Long
Dim x As Long, y As Long
With ActiveSheet
For i = 4 To .Cells(.Rows.Count, "B").End(xlUp).Row
xStr = Split(.Cells(i, "B").Value, " ")
With .Cells(i, "G")
.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 = 3
End If
Next y
Next x
End With
Next i
End With
End Sub