如何在Excel中比较两个字符串的相似度或突出显示差异?
在本文中,我们将学习如何在excel中比较两个相邻字符串以识别差异或相似之处。本文解释了两种方法,如下所述。
使用公式比较两个字符串的相似之处。
使用VBA代码比较并突出显示两个字符串的相似之处或差异。
使用公式比较两个字符串的相似之处
步骤 1 - 如下所示,采用示例数据来比较两列的字符串。

步骤 2 - 现在,在“匹配结果”列中输入以下公式,并将其拖动到需要进行数据比较的最后一行,然后按回车键。
=EXACT(A2, B2)

注意 - 在公式中,A2和B2是比较字符串的单元格。FALSE结果表示比较的字符串不同,TRUE结果表示字符串相同。

公式语法描述
参数 |
描述 |
|---|---|
EXACT(text1, text2) |
|

使用VBA代码比较并突出显示两个字符串的相似之处或差异
步骤 1 - 从键盘上按下Alt+F11键,将打开Microsoft Visual Basic for Applications窗口。

也可以使用“开发工具”选项卡打开上述编辑器,如下所示:

步骤 2 - 在Microsoft Visual Basic for Applications窗口中,双击“项目”面板中的ThisWorkbook。

步骤 3 - 现在复制下面的VBA代码,并将其输入到ThisWorkbook(代码)窗口中。
Sub highlight()
Dim xRg1 As Range
Dim xRg2 As Range
Dim xTxt As String
Dim xCell1 As Range
Dim xCell2 As Range
Dim I As Long
Dim J As Integer
Dim xLen As Integer
Dim xDiffs As Boolean
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
lOne:
Set xRg1 = Application.InputBox("Range A:", "Kutools for Excel", xTxt, , , , , 8)
If xRg1 Is Nothing Then Exit Sub
If xRg1.Columns.Count > 1 Or xRg1.Areas.Count > 1 Then
MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
GoTo lOne
End If
lTwo:
Set xRg2 = Application.InputBox("Range B:", "Kutools for Excel", "", , , , , 8)
If xRg2 Is Nothing Then Exit Sub
If xRg2.Columns.Count > 1 Or xRg2.Areas.Count > 1 Then
MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
GoTo lTwo
End If
If xRg1.CountLarge <> xRg2.CountLarge Then
MsgBox "Two selected ranges must have the same numbers of cells ", vbInformation, "Kutools for Excel"
GoTo lTwo
End If
xDiffs = (MsgBox("Click Yes to highlight similarities, click No to highlight differences ", vbYesNo + vbQuestion, "Kutools for Excel") = vbNo)
Application.ScreenUpdating = False
xRg2.Font.ColorIndex = xlAutomatic
For I = 1 To xRg1.Count
Set xCell1 = xRg1.Cells(I)
Set xCell2 = xRg2.Cells(I)
If xCell1.Value2 = xCell2.Value2 Then
If Not xDiffs Then xCell2.Font.Color = vbRed
Else
xLen = Len(xCell1.Value2)
For J = 1 To xLen
If Not xCell1.Characters(J, 1).Text = xCell2.Characters(J, 1).Text Then Exit For
Next J
If Not xDiffs Then
If J <= Len(xCell2.Value2) And J > 1 Then
xCell2.Characters(1, J - 1).Font.Color = vbRed
End If
Else
If J <= Len(xCell2.Value2) Then
xCell2.Characters(J, Len(xCell2.Value2) - J + 1).Font.Color = vbRed
End If
End If
End If
Next
Application.ScreenUpdating = True
End Sub


步骤 4 - 输入代码后,按键盘上的Alt+Q键关闭Microsoft Visual Basic for Applications窗口。
步骤 5 - 接下来,将文件保存为“启用宏的工作簿”格式。

步骤 6 - 现在按下Alt+F8运行代码。将打开以下提示:

步骤 7 - 选择宏名称,然后单击“运行”。
步骤 8 - 将打开第一个“Kutools for Excel”对话框。在这里,选择需要比较的文本字符串的第一列,然后单击“确定”按钮。

步骤 9 - 接下来,将打开第二个“Kutools for Excel”对话框以选择第二列字符串,然后单击“确定”按钮。

步骤 10 - 之后,将打开一个新的“Kutools for Excel”对话框。在这里,如果要比较字符串的相似之处,则单击“是”,如果要突出显示字符串的差异,则在下面的屏幕截图中单击“否”。

步骤 11 - 如果选择“是”,则类似的字符串将如下所示突出显示。

步骤 12 - 如果选择“否”,则不同的字符串将如下所示突出显示。

结论
因此,我们学习了两种识别excel数据中不同和相似字符串的方法。请注意,VBA代码仅限于识别某些特殊字符。例如,它无法比较包含撇号和感叹号的字符串。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP