我有兩列帶有字串,並且想知道一列中的字串的哪些部分與另一列相關。我已經檢查過這個解決方案比較 2 個文字儲存格並顯示第三個儲存格中的差異 |超級用戶但它對我的問題不起作用。
在下面的螢幕截圖中,我希望結果列包含字串,Elementary
因為這是B2
與A2
.我嘗試=SUBSTITUTE(A2,B2,"")
根據上面連結中的解決方案使用,但公式的作用是透過在兩列中顯示公共字串來執行相反的操作:
如何讓它顯示字串差異 ( Elementary
) 呢?
答案1
如何讓它顯示字串差異(基本)?
這是一個名為的自訂函數
WORDDIF
,可以執行您想要的操作。若要安裝自訂功能...
- Alt+F11 開啟 VBA 編輯器
- 從 VBA 選單中,選擇插入 > 模組
- 將以下程式碼貼到 VBA 編輯視窗中
傳回 Excel,將此公式放入 C1 中
=WORDDIF(A1,B1)
代碼:
Function WORDDIF(rngA As Range, rngB As Range) As String Dim WordsA As Variant, WordsB As Variant Dim ndxA As Long, ndxB As Long, strTemp As String WordsA = Split(rngA.Text, " ") WordsB = Split(rngB.Text, " ") For ndxB = LBound(WordsB) To UBound(WordsB) For ndxA = LBound(WordsA) To UBound(WordsA) If StrComp(WordsA(ndxA), WordsB(ndxB), vbTextCompare) = 0 Then WordsA(ndxA) = vbNullString Exit For End If Next ndxA Next ndxB For ndxA = LBound(WordsA) To UBound(WordsA) If WordsA(ndxA) <> vbNullString Then strTemp = strTemp & WordsA(ndxA) & " " Next ndxA WORDDIF = Trim(strTemp) End Function
來源https://www.mrexcel.com/forum/excel-questions/486708-compare-two-strings-find-difference.html