提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何比较两个字符串的相似性或突出差异在Excel中?

在某些情况下,您可能只需要比较两个相邻的字符串单元格,并在Excel中标记它们的相似性或差异。 本文提供了两种方法来实现它。

用公式比较两个字符串

比较两个字符串的相似性或突出与VBA代码的差异


轻松比较两个范围/工作表,并在Excel中突出显示相同/不同的单元格

点击 Kutools > 选择 > 选择相同和不同的单元格。 该 Kutools for Excel's 选择相同和不同的单元格 实用程序可帮助您轻松比较两个范围或工作表,然后在Excel中突出显示相同或不同的单元格。 看下面的截图:

Kutools for Excel 包含了比300更方便的Excel工具。 免费试用60天无限制。 立即下载免费试用版

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

用公式比较两个字符串


如下图所示,如果您只想知道比较的字符串是否匹配,则可以应用以下公式。

1。 选择一个空白单元格C2,输入公式 =精确(A2,B2) 进入公式栏,然后按Enter键。 看截图:

注意:在公式中,A2和B2是包含比较字符串的单元格。

2。 继续选择结果单元格,然后将填充句柄拖到单元格中,直到获得所有比较结果。

FALSE结果表示比较的字符串不同,而TRUE结果表示两个比较的字符串匹配。 看截图:


比较两个字符串的相似性或突出与VBA代码的差异

如果您想比较两个字符串并突出显示它们之间的相似性或差异。 以下VBA代码可以帮助您。

1。 按 其他 + F11 键同时打开 Microsoft Visual Basic for Applications 窗口。

2。 在里面 Microsoft Visual Basic for Applications 窗口中,单击 插页 > 模块。 然后将以下代码复制并粘贴到代码窗口中。

VBA代码:比较两个列字符串的相似性或突出显示差异

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

3。 按 F5 键来运行代码。 在第一个 Kutools for Excel 对话框中,选择需要比较的第一列文本字符串,然后单击 OK 按钮。

4。 然后第二个 Kutools for Excel 弹出对话框,请选择第二列字符串,然后单击 OK 按钮。

5。 在最后 Kutools for Excel 对话框中,如果要比较字符串的相似性,请单击 按钮。 要突出显示比较字符串的差异,请单击 没有 按钮。 看截图:

然后你可以看到比较结果如下图所示。


相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Stefan · 9 months ago
    Very useful script! thanks
  • To post as a guest, your comment is unpublished.
    Eddy · 11 months ago
    Hello
    How do I copy Column1 next to Column2 if one or more items in Column 1 resembles / is identical to one or more items in Column2?
    I'm sorry but I've been working with Excel VBA for a while and I can not find the answer to this question.
    Thank you in advance for your answer.
  • To post as a guest, your comment is unpublished.
    Eddy · 11 months ago
    Hello
    How do I copy Column1 next to Column2 if one or more items in Column 1 resembles / is identical to one or more items in Column2?
    I'm sorry but I've been working with Excel VBA for a while and I can not find the answer to this question.
    Thank you in advance for your answer.
  • To post as a guest, your comment is unpublished.
    Carlos · 1 years ago
    Need to know how I can identify strings with the same text format so I can link an acct to all those strings. For instance, if I have 1,000 cells with different content, I want to separate those that have format 042-XXX-XX-00 to link them to an acct#.
  • To post as a guest, your comment is unpublished.
    SDJ · 1 years ago
    Thank you!
  • To post as a guest, your comment is unpublished.
    Yogesh · 1 years ago
    Above code dose not works if any cell contains formula.
  • To post as a guest, your comment is unpublished.
    suryateja · 2 years ago
    Dear sir,
    Thank you very much for your VBA code.

    It is very much helpful.

    Actually i dont have any knowledge about coding.
    So, even to modify a little part of coding according to my requirement, i am helpless

    I have one request about this coding to make it more efficient and user friendly.
    Could you please help me??

    The main objective of above coding is to find and highlight the differences between the data in two columns for easy reference.
    But, this coding is little complicated as objective is concerned.

    Becuase, it is considering the "spaces" between the content and it is highlight the entire data even though the strings are same.
    So, if we can compare the data without spaces, then we can filter the portion of highlighted data.
    Also, instead of highlighting only the strings those which are not similar, it is hightlighting entire data right from the first different string to the data till the end of the cell.

    Hence, i would like to conclude my request as below 2 points.
    1) please edit the coding to compare the cells without consideirng the spaces
    2) please edit the coding in such a way that it can highlight only different strings but not highlighting entire para from the first different string point.

    Thank you very much for you help.

    Regards,
    Surya
    • To post as a guest, your comment is unpublished.
      Salai · 1 years ago
      i have the same request as suryateja.
      2) please edit the coding in such a way that it can highlight only different strings but not highlighting entire para from the first different string point.