LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11,058
Default Find position of first non-matching character

Try this small UDF:

Function divergence(r1 As Range, r2 As Range) As Integer
v1 = r1.Value
v2 = r2.Value
divergence = 0
i = Application.WorksheetFunction.Max(Len(v1), Len(v2))
For j = 1 To i
ch1 = Mid(v1, j, 1)
ch2 = Mid(v2, j, 1)
If ch1 = ch2 Then
Else
divergence = j
Exit Function
End If
Next
End Function

If the two strings exactly match, it will return a zero. It will also catch
case changes as well.
--
Gary''s Student - gsnu2007i


"Greg Lovern" wrote:

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
return a character at a certain position in a string of text Miranda Excel Worksheet Functions 3 May 8th 23 11:46 AM
Return position of 2nd, 3rd, ect occurrence of a character in a st jheby Excel Discussion (Misc queries) 5 April 21st 23 09:06 AM
Insert a special character in 1st and last position into a cell Mugge New Users to Excel 5 November 6th 07 09:38 PM
sort on character position Jimpm98 Excel Worksheet Functions 1 December 5th 06 12:16 PM
Return position for each matching value in entire workbook? Squidman Excel Worksheet Functions 1 September 15th 06 11:36 PM


All times are GMT +1. The time now is 02:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"