I don't know if that helps, but I am looking for a way to compare two strings and see that their differences is either a space " " or a comma ",".
For example, let say that I have one case as "Peter Smith" and the second as "peter Smiith". Here, we have a difference of an extra space and an "i".
However, I need to filter cases when both strings have the same characters except the space. For example "Peter Smith" (e.g. so, here we have a difference of an extra space only).
[COLOR=darkblue]Function[/COLOR] CHARSDIF(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]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] Len(strB)
[COLOR=darkblue]If[/COLOR] InStr(1, strA, Mid(strB, i, 1), 1) [COLOR=darkblue]Then[/COLOR]
strA = Replace(strA, Mid(strB, i, 1), "", , 1, 1)
[COLOR=darkblue]Else[/COLOR]
strTemp = strTemp & Mid(strB, i, 1)
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] i
CHARSDIF = Replace(strA & strTemp, " ", "-")
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]
Hi AlphaFrog/other,
The function works great! However what about for comparing strings of different lengths?
For example A1 - "123 KING STREET", A2 - "123 KNG ST"
I would like the output to be "IEET" or 4 so I can identify cells which are similar to each other.
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 + 1).Value = strB
rngB(r, c + 1).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 + 1).Characters(Start:=i, Length:=1).Font.ColorIndex = 3
End If
Next i, r, c
End Sub
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)
strTemp = strTemp & IIf(WordsA(ndxA) <> vbNullString, WordsA(ndxA), "-") & " "
Next ndxA
WORDDIF = Trim(strTemp)
End Function
Example:
A1="Mike,Suzy,Ted,Amber"
B1="Mike,Ted,Amber,John"
I would like
C1 to return "John"
D1 to return "Suzy"
Is this possible in excel? If its not possible, could I return both Suzy and John without returning Amber?
[COLOR=darkblue]Function[/COLOR] WORDMISS(rngA [COLOR=darkblue]As[/COLOR] Range, rngB [COLOR=darkblue]As[/COLOR] Range) [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Dim[/COLOR] WordsA [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR], WordsB [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
[COLOR=darkblue]Dim[/COLOR] ndxB [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], strTemp As [COLOR=darkblue]String[/COLOR]
strDelimter = "," [COLOR=green]'Change word delimiter to suit[/COLOR]
WordsA = strDelimter & rngA.Text & strDelimter
WordsB = Split(rngB.Text, strDelimter)
[COLOR=darkblue]For[/COLOR] ndxB = [COLOR=darkblue]LBound[/COLOR](WordsB) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](WordsB)
[COLOR=darkblue]If[/COLOR] InStr(1, WordsA, strDelimter & WordsB(ndxB) & strDelimter, vbTextCompare) = 0 [COLOR=darkblue]Then[/COLOR]
strTemp = strTemp & strDelimter & WordsB(ndxB)
[COLOR=darkblue]Else[/COLOR]
WordsA = Replace(WordsA, WordsB(ndxB), vbNullString, 1, 1)
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] ndxB
[COLOR=darkblue]If[/COLOR] Len(strTemp) [COLOR=darkblue]Then[/COLOR] WORDMISS = Mid(strTemp, 2)
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Function[/COLOR]