[color=darkblue]Sub[/color] CHARDIFSabde()
[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] Long, col [color=darkblue]As[/color] [color=darkblue]Variant[/color]
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
[color=darkblue]For[/color] [color=darkblue]Each[/color] col [color=darkblue]In[/color] Array("A", "D")
[color=darkblue]Set[/color] rngA = Range(col & "1", Range(col & Rows.Count).End(xlUp))
[color=darkblue]Set[/color] rngB = rngA.Offset(, 1)
[color=darkblue]For[/color] r = 1 [color=darkblue]To[/color] rngA.Rows.Count
strA = rngA(r, 1).Value
strB = rngB(r, 1).Value
rngB(r, 2).Value = strB
rngB(r, 2).Font.ColorIndex = xlAutomatic
[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, 2).Characters(Start:=i, Length:=1).Font.ColorIndex = 3
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]Next[/color] i, r, col
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
Sub CHARDIFSabde()
Dim rngA As Range, rngB As Range
Dim strA As String, strB As String
Dim i As Long, r As Long, col As Variant
On Error Resume Next
For Each col In Array("A", "D")
Set rngA = Range(col & "3", Range(col & Rows.Count).End(xlUp))
Set rngB = rngA.Offset(, 1)
For r = 1 To rngA.Rows.Count
strA = rngA(r, 1).Value
strB = rngB(r, 1).Value
[COLOR=#ff0000] rngB(r, 2).Value = IIf(IsNumeric(strB), "'", "") & strB[/COLOR]
rngB(r, 2).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, 2).Characters(Start:=i, Length:=1).Font.ColorIndex = 3
End If
Next i, r, col
End Sub
You're welcome.
Please use CODE tags when posting your code. It makes reading it much easier. See my signature block below.
Code:Sub CHARDIFSabde() Dim rngA As Range, rngB As Range Dim strA As String, strB As String Dim i As Long, r As Long, col As Variant On Error Resume Next For Each col In Array("A", "D") Set rngA = Range(col & "3", Range(col & Rows.Count).End(xlUp)) Set rngB = rngA.Offset(, 1) For r = 1 To rngA.Rows.Count strA = rngA(r, 1).Value strB = rngB(r, 1).Value [COLOR=#ff0000] rngB(r, 2).Value = IIf(IsNumeric(strB), "'", "") & strB[/COLOR] rngB(r, 2).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, 2).Characters(Start:=i, Length:=1).Font.ColorIndex = 3 End If Next i, r, col End Sub
Function CHARDIF(rngA As Range, rngB As Range) As String
Dim strA As String, strB As String
Dim i As Long, strTemp As String
strA = rngA.Value
strB = rngB.Value
If Len(strA) > Len(strB) Then
strTemp = strA
strA = strB
strB = strTemp
strTemp = ""
End If
For i = 1 To Len(strB)
If i > Len(strA) Then
strTemp = strTemp & Mid(strB, i, 1)
ElseIf UCase(Mid(strA, i, 1)) <> UCase(Mid(strB, i, 1)) Then
strTemp = strTemp & Mid(strB, i, 1)
End If
Next i
CHARDIF = strTemp
End Function
[color=darkblue]Function[/color] DIGIDIF(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 [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Dim[/color] bFirstDiff As [color=darkblue]Boolean[/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] bFirstDiff [color=darkblue]Or[/color] UCase(Mid(strA, i, 1)) <> UCase(Mid(strB, i, 1)) [color=darkblue]Then[/color]
strTemp = strTemp & Mid(strB, i, 1)
bFirstDiff = [color=darkblue]True[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]Next[/color] i
DIGIDIF = strTemp
[color=darkblue]End[/color] [color=darkblue]Function[/color]
Hi AlphaFrog,
Sorry to bring out this post again! I have tried the WORDIF and CHARIF functions and it works great, but not sure if I can use vba to do the following in a Sub:
String1: Hi John, this is Elaine, how was your day?
String2: Hi John. This is Elaine, was day?
The program should be able to highlight the "," after John in String1, highlight the "t" in the word "this" in String1, and show the strikeout effect on the word "how" and "your" in String1, to show the differences String2 has against String1.
Not sure if this is possible, please enlighten me on this thanks!