Skip to content

Instr - Built-in String Function

Purpose

InStr returns the starting position of the first occurrence of one string within another.

Syntax

Instr(str1, str2 [,int] [,bool])

Arguments

Argument Required Description
str1 Yes Any valid string expression. The string being searched.
str2 Yes Any valid string expression containing the search string.
int No Any valid integer expression containing the starting position of the search.
bool No Any valid Boolean expression indicating if the search is case insensitive.

Returns

Variant with a string (VT_BSTR) value

Example

Let iPos = Instr("AbraCadaBra", "b")
Return iPos // returns 2
// Example starting point:
Let iPos = Instr("AbraCadaBra", "r", 3)
Return iPos // returns 3
// Example case sensitive search:
Let iPos = Instr("AbraCadaBra", "B")
Return iPos // returns 9
// Example case insensitive search:
Let iPos = Instr("AbraCadaBra", "B", 0, true)
Return iPos // returns 2

See Also

Notes

  • When the arguments cannot be converted to the specified type, a runtime error is generated.
  • If int is omitted or passed as zero (0), the search will begin with the first character.
  • If int is larger than the length of str1, the result will be 0, indicating nothing found.
  • If bool is omitted, the search will be case-sensitive.
  • If str2 is not found, the function returns 0.