跳至主要内容

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

Author: Siluvia Last Modified: 2025-05-07

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

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功能!立即获取


相关文章:

最佳办公效率工具

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

使用 Kutools for Excel 提升您的 Excel 技能,体验前所未有的高效。 Kutools for Excel 提供超过 300 种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!