InStr, InStrB

Returns a Variant (Long) specifying the position of the first occurrence of one string within another.

Syntax:

  • InStr( [ start , ] string1, string2 [ , compare ] )
  • InStrB( [ start , ] string1, string2 [ , compare ] )
start
optional Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. start must be 1 or greater. The start argument is required if compare is specified.
string1
required String expression being searched.
string2
required String expression sought.
compare
optional Specifies the type of string comparison. If compare is omitted, the Option Compare setting determines the type of comparison. Specify a valid LCID (LocaleID) to use locale-specific rules in the comparison.

The compare argument settings are:

Constant Value Description
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.

Return values: the first row that applies gives the result.

If InStr returns
string1 is Null Null
string2 is Null Null
string1 is zero-length 0
string2 is zero-length start, even when start is greater than Len(string1)
start > Len(string1) 0
string2 is not found 0
string2 is found within string1 Position at which match is found

The InStrB function is used with byte data contained in a string. Instead of returning the character position of the first occurrence of one string within another, InStrB returns the byte position.

Common Errors

Error When
5 (&H5), Invalid procedure call or argument start is less than 1
InStr is given a start greater than 1,073,741,824
InStr is given a compare that is less than 0, or greater than 1 and not a valid locale identifier
InStr is given a compare that is not a valid locale identifier, and string1 or string2 is zero-length
94 (&H5E), Invalid use of Null start or compare is Null

Note BETA 1034

When InStr is given a compare that is not a valid locale identifier, and string1 or string2 is zero-length, VB6 and 64-bit VBA raise no error.

Note BETA 1034

When InStr is given a compare of 2 (vbDatabaseCompare), VB6 raises error 5 (&H5), Invalid procedure call or argument; twinBASIC raises none.

Example

This example uses the InStr function to return the position of the first occurrence of one string within another.

Dim SearchString As String, SearchChar As String
SearchString = "XXpXXpXXPXXP"    ' String to search in.
SearchChar = "P"                 ' Search for "P".

' A textual comparison starting at position 4.
Debug.Print InStr(4, SearchString, SearchChar, 1)    ' 6

' A binary comparison starting at position 1.
Debug.Print InStr(1, SearchString, SearchChar, 0)    ' 9

' Comparison is binary by default (last argument is omitted).
Debug.Print InStr(SearchString, SearchChar)          ' 9

Debug.Print InStr(1, SearchString, "W")              ' 0

This example shows the rows of the return-value table that are easy to get wrong.

Debug.Print InStr(3, "abcdef", "cd")      ' 3
Debug.Print InStr(4, "abc", "c")          ' 0
Debug.Print InStr(5, "abc", "")           ' 5
Debug.Print InStr(1, "", "")              ' 0
Debug.Print IsNull(InStr(1, Null, "a"))   ' True

On Error Resume Next
Debug.Print InStr(0, "abc", "a")
Debug.Print Err.Number                    ' 5

See Also