提示:其他語言是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解決了您的大多數問題,並使您的生產率提高了80%
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文字; 將數字和貨幣轉換為英文單詞。
  • 合併工具:將多個工作簿和工作表合二為一; 合併多個單元格/行/列,而不會丟失數據; 合併重複的行和總和。
  • 拆分工具:根據價值將數據分割成多個工作表; 一本工作簿可轉換為多個Excel,PDF或CSV文件; 一列到多列。
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 將個性化電子郵件批量發送給多個收件人。
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 與Office 2007-2019和365一起使用; 支持所有語言; 在您的企業或組織中輕鬆部署。

用公式比較兩個字符串


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

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。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
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 · 1 years ago
    Very useful script! thanks
  • To post as a guest, your comment is unpublished.
    Eddy · 1 years 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 · 1 years 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 · 2 years ago
    Thank you!
  • To post as a guest, your comment is unpublished.
    Yogesh · 2 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 · 2 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.