跳至主要内容

Kutools for Office — 一套工具,五种功能。事半功倍。

如何比较两列并在Excel中突出显示不匹配的数据?

Author: Siluvia Last Modified: 2025-08-22

如下截图所示,要比较两列相邻单元格并突出显示不匹配的数据,您可以尝试本文中的方法。

Screenshot showing Excel columns with highlighted unmatched data

使用VBA代码比较两列并突出显示不匹配的数据
使用Kutools for Excel比较两列并突出显示不匹配的数据


使用VBA代码比较两列并突出显示不匹配的数据

如上面的截图所示,您正在比较A列和B列中的数据。请运行以下VBA代码以突出显示不匹配的数据。

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

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

VBA代码:比较两列并突出显示不匹配的数据

Sub Dyeware()
    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 ranges must have the same numbers of cells ", vbInformation, "Kutools for Excel"
       GoTo lTwo
    End If
    xDiffs = (MsgBox("Click Yes to highlight matched data, click No to highlight unmatched data ", vbYesNo + vbQuestion, "Kutools for Excel") = vbNo)
    Application.ScreenUpdating = False
    xRg2.Interior.ColorIndex = xlNo
    xRg1.Interior.ColorIndex = xlNo
    For I = 1 To xRg1.Count
        Set xCell1 = xRg1.Cells(I)
        Set xCell2 = xRg2.Cells(I)
        If xCell1.Value = xCell2.Value Then
            If Not xDiffs Then
                xCell1.Interior.Color = vbRed
                xCell2.Interior.Color = vbRed
            End If
        Else
            If xDiffs Then
                xCell1.Interior.Color = vbRed
                xCell2.Interior.Color = vbRed
            End If
        End If
    Next
    Application.ScreenUpdating = True
End Sub

3. 按 F5 键运行代码。在弹出的 Kutools for Excel 对话框中,选择需要比较的第一列,然后单击 确定 按钮。参见截图。

Screenshot of the first Kutools dialog for selecting the first comparison column

4. 然后在下一个弹出的 Kutools for Excel 对话框中选择第二列进行比较。

Screenshot of the second Kutools dialog for selecting the second comparison column

5. 在如下截图所示的对话框中,为了突出显示不匹配的数据,请单击 按钮。

Screenshot of the dialog prompting to highlight matched or unmatched data

随后,两列之间的所有不匹配数据都会立即被突出显示。参见截图:

Screenshot showing highlighted unmatched data


使用Kutools for Excel比较两列并突出显示不匹配的数据

如果您只想找出并突出显示某一列中的单元格值是否存在于另一列中,例如,如下截图所示,A列中的数字2不存在于B列中。您可以尝试 Kutools for ExcelCompare Ranges 工具。

Screenshot showing data in columns with an example of an unmatched cell in column A

Kutools for Excel 提供了超过 300 种高级功能,简化复杂任务,提升创造力与效率。 通过集成 AI 能力,Kutools 能够精准自动执行任务,让数据管理变得轻松简单。Kutools for Excel 的详细信息...         免费试用...

1. 单击 Kutools > 插入 > 选择相同和不同单元格。参见截图:

Screenshot of Kutools' Select Same & Different Cells option in Excel

2. 在“选择相同和不同单元格”对话框中,您需要进行如下配置:

2.1 在“ 查找值”框中选择包含不匹配单元格的第一列;
2.2 在“根据”框中选择要基于其进行比较的第二列;
2.3 在“基于”部分,选择“按单元格”选项;
2.3 在“查找”部分选择“不同值”选项;
2.4 在“对选中结果的处理”部分,勾选“填充背景颜色”框,并从下拉列表中指定一种背景色;
2.5 单击 确定 按钮。参见截图:

Screenshot of the Select Same & Different Cells dialog

3. 然后会弹出一个对话框,告诉您有多少个仅在A列而不在B列的单元格,单击 确定 按钮,所有匹配的单元格都会在A列中被选中。

Kutools for Excel - 通过超过300个必备工具,让Excel功能大幅提升。永久免费享受AI功能!立即获取


相关文章:

最佳Office办公效率工具

🤖 Kutools AI 助手:以智能执行为基础,彻底革新数据分析 |代码生成 |自定义公式创建|数据分析与图表生成 |调用Kutools函数……
热门功能:查找、选中项的背景色或标记重复项 | 删除空行 | 合并列或单元格且不丢失数据 | 四舍五入……
高级LOOKUP多条件VLookup|多值VLookup|多表查找|模糊查找……
高级下拉列表快速创建下拉列表 |依赖下拉列表 | 多选下拉列表……
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域与列……
特色功能网格聚焦 |设计视图 | 增强编辑栏 | 工作簿及工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按名单发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/倾斜/删除线等)……
15大工具集12项 文本工具添加文本删除特定字符等)|50+种 图表 类型甘特图等)|40+实用 公式基于生日计算年龄等)|19项 插入工具插入二维码从路径插入图片等)|12项 转换工具小写金额转大写汇率转换等)|7项 合并与分割工具高级合并行分割单元格等)| ……
Kutools支持多种语言——可选择英语、西班牙语、德语、法语、中文等40多种语言!

通过Kutools for Excel提升您的Excel技能,体验前所未有的高效办公。 Kutools for Excel提供300多项高级功能,助您提升效率并节省时间。 点击此处获取您最需要的功能……


Office Tab为Office带来多标签界面,让您的工作更加轻松

  • 支持在Word、Excel、PowerPoint中进行多标签编辑与阅读
  • 在同一个窗口的新标签页中打开和创建多个文档,而不是分多个窗口。
  • 可提升50%的工作效率,每天为您减少数百次鼠标点击!

所有Kutools加载项,一键安装

Kutools for Office套件包含Excel、Word、Outlook和PowerPoint的插件,以及Office Tab Pro,非常适合跨Office应用团队使用。

Excel Word Outlook Tabs PowerPoint
  • 全能套装——Excel、Word、Outlook和PowerPoint插件+Office Tab Pro
  • 单一安装包、单一授权——数分钟即可完成设置(支持MSI)
  • 协同更高效——提升Office应用间的整体工作效率
  • 30天全功能试用——无需注册,无需信用卡
  • 超高性价比——比单独购买更实惠