如何在Excel中比較兩個字串的相似度或突出顯示差異?
在本文中,我們將學習如何在excel中比較兩個相鄰字串以識別差異或相似之處。本文解釋了兩種方法,如下所述。
使用公式比較兩個字串的相似之處。
使用VBA程式碼比較並突出顯示兩個字串的相似之處或差異。
使用公式比較兩個字串的相似之處
步驟 1 - 如下所示,採用示例資料來比較兩列的字串。

步驟 2 - 現在,在“匹配結果”列中輸入以下公式,並將其拖動到需要進行資料比較的最後一行,然後按回車鍵。
=EXACT(A2, B2)

注意 - 在公式中,A2和B2是比較字串的單元格。FALSE結果表示比較的字串不同,TRUE結果表示字串相同。

公式語法描述
引數 |
描述 |
|---|---|
EXACT(text1, text2) |
|

使用VBA程式碼比較並突出顯示兩個字串的相似之處或差異
步驟 1 - 從鍵盤上按下Alt+F11鍵,將開啟Microsoft Visual Basic for Applications視窗。

也可以使用“開發工具”選項卡開啟上述編輯器,如下所示:

步驟 2 - 在Microsoft Visual Basic for Applications視窗中,雙擊“專案”面板中的ThisWorkbook。

步驟 3 - 現在複製下面的VBA程式碼,並將其輸入到ThisWorkbook(程式碼)視窗中。
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


步驟 4 - 輸入程式碼後,按鍵盤上的Alt+Q鍵關閉Microsoft Visual Basic for Applications視窗。
步驟 5 - 接下來,將檔案儲存為“啟用宏的工作簿”格式。

步驟 6 - 現在按下Alt+F8執行程式碼。將開啟以下提示:

步驟 7 - 選擇宏名稱,然後單擊“執行”。
步驟 8 - 將開啟第一個“Kutools for Excel”對話方塊。在這裡,選擇需要比較的文字字串的第一列,然後單擊“確定”按鈕。

步驟 9 - 接下來,將開啟第二個“Kutools for Excel”對話方塊以選擇第二列字串,然後單擊“確定”按鈕。

步驟 10 - 之後,將開啟一個新的“Kutools for Excel”對話方塊。在這裡,如果要比較字串的相似之處,則單擊“是”,如果要突出顯示字串的差異,則在下面的螢幕截圖中單擊“否”。

步驟 11 - 如果選擇“是”,則類似的字串將如下所示突出顯示。

步驟 12 - 如果選擇“否”,則不同的字串將如下所示突出顯示。

結論
因此,我們學習了兩種識別excel資料中不同和相似字串的方法。請注意,VBA程式碼僅限於識別某些特殊字元。例如,它無法比較包含撇號和感嘆號的字串。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP