如何在Excel中比较两个字符串的相似性或突出差异?
在本文中,我们将学习如何比较Excel中的两个相邻字符串以识别差异或相似之处。正如下面所述,本文解释了两种方法。
使用公式比较两个字符串的相似之处。
使用VBA代码比较和突出显示两个字符串的相似性或差异。
使用公式比较两个字符串的相似性
第1步 − 以下方案数据用于比较两列的字符串。
第2步 − 现在,在Match Result列中输入以下公式,并将其拖动到所需数据比较的最后一行,然后按Enter键。
**=EXACT(A2, B2) **
注意 − 公式中,A2和B2是比较字符串的单元格。FALSE结果表示比较的字符串不同,而TRUE结果表示相似的字符串。
公式语法描述
参数 | 描述 |
---|---|
EXACT(text1, text2) |
- Text1 这是需要与另一个字符串进行比较的第一个字符串。
- Text2 这是需要与Text1进行比较的第二个字符串。
使用VBA代码比较和突出显示两个字符串的相似性或差异
第1步 − 从键盘上按下Alt+F11键,Microsoft Visual Basic for Applications窗口将会打开。
还可以使用开发人员选项卡打开上面的编辑器,如下所示−
第2步 − 在Microsoft Visual Basic for Applications窗口中,双击Project面板中的ThisWorkbook。
第3步 − 现在复制以下VBA代码,并将其输入到ThisWorkbook(Code)窗口中。
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 − 接下来,另存为 Excel 宏工作簿格式。
步骤 6 − 现在按 Alt+F8 运行代码。会出现以下提示 –
步骤 7 −选择宏名称并单击“运行”按钮。
步骤 8 − 第一个 Kutools for Excel 对话框将打开。在此处,选择需要比较的文本字符串的第一列,然后单击“确定”按钮。
步骤 9 − 接下来,第二个 Kutools for Excel 对话框将打开,以选择第二列字符串,并单击“确定”按钮。
步骤 10 − 然后会出现一个新的 Kutools for Excel 对话框。在此处,如果您想比较相似的字符串,则单击“是”,如果您想突出显示字符串的差异,则单击下面的“否”截图。
步骤 11 − 如果选择“是”,则相似的字符串将突出显示如下。
步骤12 − 如果选择“否”(No),不同的字符串将如下所示被突出显示。
结论
因此,我们学习了两种方法来识别Excel数据中不同和相似的字符串。请注意,VBA代码只能识别一些特殊字符。例如,无法比较包含撇号和感叹号标记的字符串。