thoroughly-baffled by InStr function w/ binary compare

T

terry b

Hello, all
[Using the simple example from the vba help files, the following
returns 9:]
Sub StringPractice()
Dim SearchString, SearchChar, MyPos
SearchString = "XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
' A binary comparison starting at position 1. Returns 9.
MyPos = InStr(1 , SearchString, SearchChar, 0)
MsgBox MyPos
End Sub

Obviously the first occurrence of "P" is in the 9 pos when starting at
beginning of SearchString... BUT WHY, when I substitute any other
number for a starting position, click on reset, then run sub again,
does the answer ALWAYS RETURN A 9??? Does the constant vbBinaryCompare
not support the use a starting-position number w/ InStr???
Thanks much for your time [:))

terry b.
 
R

RWN

Unless your starting # is > 9 then it will always return 9.
The starting position is just that, the start position for the search.
Start in pos one, finds the 1st instance of "P" at pos 9.
Start in pos nine, finds the 1st instance of "P" at pos 9 (same "P").
Start in pos ten, finds the 1st instance of "P" (from pos 10 to the end of the string) at
pos 12 ( "P").
 
T

terry b

Hello, Rob
I have no clue as to why I was ASSUMING that the position the
sought-after string would be found would be RELATIVE to the start
position! Thanks much for clearing up that little problem!
Regards,
terry b
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top