Here's a custom function called WORDDIF that may do what you want.
To install the custom function...
- Alt+F11 to open the VBA Editor
- From the VBA menu, select Insert\ Module
- Paste the code below in the VBA Edit window
Back in Excel, put this formula in C1
=WORDDIF(A1,B1)
Code: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
[COLOR=darkblue]Function[/COLOR] CHARDIF(rngA [COLOR=darkblue]As[/COLOR] Range, rngB [COLOR=darkblue]As[/COLOR] Range) [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] strA [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], strB [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], strTemp As [COLOR=darkblue]String[/COLOR]
strA = rngA.Value
strB = rngB.Value
[COLOR=darkblue]If[/COLOR] Len(strA) > Len(strB) [COLOR=darkblue]Then[/COLOR]
strTemp = strA
strA = strB
strB = strTemp
strTemp = ""
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] Len(strB)
[COLOR=darkblue]If[/COLOR] i > Len(strA) [COLOR=darkblue]Then[/COLOR]
strTemp = strTemp & Mid(strB, i, 1)
[COLOR=darkblue]ElseIf[/COLOR] UCase(Mid(strA, i, 1)) <> UCase(Mid(strB, i, 1)) [COLOR=darkblue]Then[/COLOR]
strTemp = strTemp & Mid(strB, i, 1)
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] i
CHARDIF = strTemp
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]
The WORDDIF function compares words and not individual characters. It returned ABC in your example because it evaluates ABC as a word that doesn't exist in the sentence ABCDEF.
This CHARDIF function does a case insensitive 1-to-1 character comparison.
Code:[COLOR=darkblue]Function[/COLOR] CHARDIF(rngA [COLOR=darkblue]As[/COLOR] Range, rngB [COLOR=darkblue]As[/COLOR] Range) [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] [COLOR=darkblue]Dim[/COLOR] strA [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], strB [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], strTemp As [COLOR=darkblue]String[/COLOR] strA = rngA.Value strB = rngB.Value [COLOR=darkblue]If[/COLOR] Len(strA) > Len(strB) [COLOR=darkblue]Then[/COLOR] strTemp = strA strA = strB strB = strTemp strTemp = "" [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] Len(strB) [COLOR=darkblue]If[/COLOR] i > Len(strA) [COLOR=darkblue]Then[/COLOR] strTemp = strTemp & Mid(strB, i, 1) [COLOR=darkblue]ElseIf[/COLOR] UCase(Mid(strA, i, 1)) <> UCase(Mid(strB, i, 1)) [COLOR=darkblue]Then[/COLOR] strTemp = strTemp & Mid(strB, i, 1) [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Next[/COLOR] i CHARDIF = strTemp [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]
[COLOR=darkblue]Sub[/COLOR] CHARDIFS()
[COLOR=darkblue]Dim[/COLOR] rngA [COLOR=darkblue]As[/COLOR] Range, rngB [COLOR=darkblue]As[/COLOR] Range
[COLOR=darkblue]Dim[/COLOR] strA [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], strB [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], r [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], c [COLOR=darkblue]As[/COLOR] Long
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
[COLOR=darkblue]Set[/COLOR] rngA = Application.InputBox("Select the 1st cell(s) to compare to.", "Select Range A", Type:=8)
[COLOR=darkblue]If[/COLOR] rngA [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]Do[/COLOR]
[COLOR=darkblue]Set[/COLOR] rngB = Application.InputBox("Select the 2nd cell(s) to compare.", "Select Range B", Type:=8)
[COLOR=darkblue]If[/COLOR] rngB [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[COLOR=darkblue]If[/COLOR] rngA.Columns.Count = rngB.Columns.Count And rngA.Rows.Count = rngB.Rows.Count [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Do[/COLOR]
MsgBox "Range B muct have the same number of rows and columns as Range A.", vbExclamation, "Different Size Ranges"
[COLOR=darkblue]Set[/COLOR] rngB = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Loop[/COLOR]
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
[COLOR=darkblue]For[/COLOR] c = 1 [COLOR=darkblue]To[/COLOR] rngA.Columns.Count
[COLOR=darkblue]For[/COLOR] r = 1 [COLOR=darkblue]To[/COLOR] rngA.Rows.Count
strA = rngA(r, c).Value
strB = rngB(r, c).Value
[COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] Len(strB)
[COLOR=darkblue]If[/COLOR] i > Len(strA) [COLOR=darkblue]Or[/COLOR] UCase(Mid(strA, i, 1)) <> UCase(Mid(strB, i, 1)) [COLOR=darkblue]Then[/COLOR]
rngB(r, c).Characters(Start:=i, Length:=1).Font.ColorIndex = 3
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] i, r, c
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
Sub CHARDIFS()
Dim rngA As Range, rngB As Range
Dim strA As String, strB As String
Dim i As Long, r As Long, c As Long
On Error Resume Next
Set rngA = Application.InputBox("Select the 1st cell(s) to compare to.", "Select Range A", Type:=8)
If rngA Is Nothing Then Exit Sub
Do
Set rngB = Application.InputBox("Select the 2nd cell(s) to compare.", "Select Range B", Type:=8)
If rngB Is Nothing Then Exit Sub
If rngA.Columns.Count = rngB.Columns.Count And rngA.Rows.Count = rngB.Rows.Count Then Exit Do
MsgBox "Range B muct have the same number of rows and columns as Range A.", vbExclamation, "Different Size Ranges"
Set rngB = Nothing
Loop
On Error Resume Next
For c = 1 To rngA.Columns.Count
For r = 1 To rngA.Rows.Count
strA = rngA(r, c).Value
strB = rngB(r, c).Value
rngB(r, c [COLOR=#ff0000]+ 1[/COLOR]).Value = strB
rngB(r, c [COLOR=#ff0000]+ 1[/COLOR]).Font.ColorIndex = xlAutomatic
For i = 1 To Len(strB)
If i > Len(strA) Or UCase(Mid(strA, i, 1)) <> UCase(Mid(strB, i, 1)) Then
rngB(r, c[COLOR=#ff0000] + 1[/COLOR]).Characters(Start:=i, Length:=1).Font.ColorIndex = 3
End If
Next i, r, c
End Sub