Skip to main content

如何在Excel中比較兩個字串的相似性或突出顯示差異?

Author: Siluvia Last Modified: 2025-08-06

在某些情況下,您可能只需要比較兩個相鄰的字串單元格,並在Excel中標記它們的相似性或差異。本文提供了兩種方法供您實現這一目標。

使用公式比較兩個字串
使用VBA代碼比較兩個字串的相似性或突出顯示差異


使用公式比較兩個字串

如下截圖所示,如果您只想知道被比較的字串是否匹配,可以應用以下公式。

sample data

1. 選擇一個空白單元格C2,將公式 =EXACT(A2, B2) 輸入到編輯欄(F)中,然後按Enter鍵。參見截圖:

Compare two strings with a formula

備註:在公式中,A2和B2是包含要比較字串的單元格。

2. 繼續選擇結果單元格,然後拖動填充柄至其他單元格,直到獲得所有比較結果。

FALSE結果表示被比較的字串不同,TRUE結果則表示兩個被比較的字串匹配。參見截圖:

drag the formula down to get the compared results

a screenshot of kutools for excel ai

使用 Kutools AI 解鎖 Excel 的魔法

  • 智能執行:執行單元格操作、分析數據並創建圖表——所有這些都由簡單的指令驅動。
  • 自訂公式:生成量身定制的公式,簡化您的工作流程。
  • VBA 編碼:輕鬆編寫和實現 VBA 代碼。
  • 公式解釋:輕鬆理解複雜的公式。
  • 文本翻譯:打破電子表格中的語言障礙。
通過人工智能工具增強您的 Excel 能力。立即下載,體驗前所未有的效率!

使用VBA代碼比較兩個字串的相似性或突出顯示差異

如果您想比較兩個字串並突出顯示它們之間的相似性或差異,以下VBA代碼可以幫助您。

1. 同時按下Alt + 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對話框中,選擇需要比較的第一列文本字串,然後點擊確定按鈕。

vba code to select the first list

4. 接著第二個Kutools for Excel對話框彈出,請選擇第二列字串,然後點擊確定按鈕。

vba code to select the second list

5. 在最後一個Kutools for Excel對話框中,如果要比較字串的相似性,點擊按鈕;若要突出顯示被比較字串的差異,點擊按鈕。參見截圖:

select the yes or no button to your need

然後您可以像下面的截圖一樣看到比較結果。

the similarity or differences will be highlighted


相關文章:

最佳 Office 辦公效率工具

🤖 Kutools AI Aide:徹底革新數據分析,基於智能執行|生成程式碼|創建自訂公式|分析數據並生成圖表|調用 Kutools Functions
熱門功能查找、選取項目的背景色或標記重複值 | 刪除空行 | 合併列或單元格且不丟失資料 | 四捨五入...
高級 LOOKUP多條件查找|多值查找|多表查找|模糊查找...
高級下拉列表快速創建下拉列表 | 關聯下拉列表 | 多選下拉列表 ...
列管理器添加指定數量的列 | 移動列 | 切換隱藏列的可見狀態 | 區域與列比較 ...
精選功能網格聚焦 | 設計檢視 | 增強編輯欄 | 工作簿及工作表管理器 | 資源庫 (自動文本) | 日期提取器 | 合併資料 | 加密/解密儲存格 | 按清單發送電子郵件 | 超級篩選 | 特殊篩選(篩選粗體/傾斜/刪除線 ...)...
前15 大工具集12 款文本工具添加文本刪除特定字符,...)|50+ 種圖表 類型甘特圖,...)|40+ 實用 公式基於生日計算年齡,...)|19 款插入工具插入QR码按路徑插入圖片,...)|12 款轉換工具金額轉大寫匯率轉換,...)|7 款合併和分割工具高級合併行分割儲存格,...)| ...以及更多
使用 Kutools 支援你的語言——支援英語、西班牙語、德語、法語、中文及40 多種語言!

利用 Kutools for Excel 大幅提升你的 Excel 技能,感受前所未有的高效體驗。 Kutools for Excel 提供超過300 項高級功能,助你提升效率並保存時間。 點此查看你最需要的功能...


Office Tab 為 Office 帶來標籤式介面,讓你的工作更加輕鬆

  • 啟用 Word、Excel、PowerPoint 的標籤式編輯和閱讀功能
  • 在同一個視窗的標籤中打開和創建多個文件,而不是在新窗口中分開開啟。
  • 可提升你50% 的工作效率,每天為你大量減少滑鼠點擊次數!