Skip to main content

如何比較兩列並在Excel中突出顯示不匹配的數據?

Author: Siluvia Last Modified: 2025-05-12

如下截圖所示,為了比較兩列相鄰單元格並突出顯示不匹配的數據,您可以嘗試本文中的方法。

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 Excel比較區域工具。

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 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!