What is the syntax to return the index of a string found within another string?

InStr (start integer, string1, string2, compare)
‘start integer – specifies starting position for search; optional parameter
‘string1 – string to be searched; mandatory parameter
‘string2 – indicate string value; mandatory parameter
‘compare - sets string comparison between 0 for binary comparison (default) or 1 for text comparison; optional
Dim name As String
name = "Steve Gates"
MsgBox (InStr(1, name, "S")) 'returns 1
MsgBox (InStr(7, name, "hello")) 'returns 0
MsgBox (InStr(1, name, "Gates")) 'returns 7
MsgBox (InStr(name, "Steve")) 'returns 1

Be aware: spaces count as a character, a string’s index starts at 1, and a 0 yield means the string is not contained. The search occurs from left->right. For a right->left search, use InStrRev.

Published by