View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Charles Williams Charles Williams is offline
external usenet poster
 
Posts: 968
Default Find position of first non-matching character

You could try this UDF:

Option Explicit

Public Function MissMatch(String1 As Variant, String2 As Variant)
Dim aByte1() As Byte
Dim aByte2() As Byte

Dim k1 As Long
Dim k2 As Long


aByte1 = CStr(String1)
aByte2 = CStr(String2)
MissMatch = 0
If Len(CStr(String1)) 0 Then
For k1 = LBound(aByte1) To UBound(aByte1) Step 2
k2 = k2 + 1
If k1 UBound(aByte2) Then
MissMatch = k2
Exit For
Else
If aByte1(k1) < aByte2(k1) Then
MissMatch = k2
Exit For
End If
End If
Next k1
End If
End Function


Charles
__________________________________________________
The Excel Calculation Site
http://www.decisionmodels.com
"Greg Lovern" wrote in message
...
I have two strings that start with the same characters, but at some
unknown and varying point they begin to differ. I want the position
number of the first non-matching character.

For example:
-- table and chair
-- table plus chair
-- position of first non-matching character is 7 (the "a" in
"and", and the "p" in "plus").

In the above example, both strings start out "table ", then start to
differ beginning with the 7th character. How can I return the position
of the first non-matching character, which in this example is 7?


Thanks,

Greg