View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default String Comparison

This is a little strange because you don't get a lot of useful information.
You don't know which postitions the characters are differrent but here is the
solution anyway

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/2/2008 by Joel
'

'
RowCount = 1
Do While Range("B" & RowCount) < ""
If Range("B" & RowCount) < Range("C" & RowCount) Then

CompareStr = €œ€
For i = 1 To Len(Range("D" & RowCount))
If Mid(Range("B" & RowCount), i, 1) < _
Mid(Range("C" & RowCount), i, 1) Then

CompareStr = CompareStr & Mid(Range("B" & RowCount), i, 1)
End If
Next i
Range("D" & RowCount) = CompareStr
End If
RowCount = RowCount + 1
Loop
End Sub


" wrote:

On Jul 2, 12:56 pm, Joel wrote:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/2/2008 by Joel
'

'
RowCount = 1
Do While Range("B" & RowCount) < ""
If Range("B" & RowCount) < Range("C" & RowCount) Then

'copy cell B to column D
Range("D" & RowCount).Value = _
Range("B" & RowCount).Value
'highlight characters that are different
For i = 1 To Len(Range("D" & RowCount))
If Mid(Range("B" & RowCount), i, 1) < _
Mid(Range("C" & RowCount), i, 1) Then

Range("D" & RowCount). _
Characters(Start:=i, Length:=1).Font _
.ColorIndex = 41
End If
Next i
End If
RowCount = RowCount + 1
Loop
End Sub



" wrote:
Hi


I have 2 text strings of equal length (7) in the cells for columns B &
C. The strings in C differ slightly to those in B.


I am a novice at macros but would like some help in comparing column C
cells to B cells and returning the difference in Column D. i.e.
Compare C2 to B2 and return the difference in D2 and continue down the
rows until the end of the data.


The first row contains the column headings.


Any help wolud be much appreciated.


Thanks- Hide quoted text -


- Show quoted text -


Thanks Joel.
That was a step in the right direction. However it only highlighted
the difference in Column D. What i want done is return the different
characters. e.g.:
B C D
2 ZSZ80KT Z528OKT S0
3 ZGY92GG ZGY926G G
4 YYS3WDE YYS3WOE D

In this case comparing C2 to B2 we find that B2 has 'S' and '0'
different in C2 so these two characters are returned in D2 and C4 to
B4, 'D' is different in C4 so o'D' is returned in D4 and so on. Rob
Allchurch, does this help?

Thanks again.