Saturday, July 23, 2016

How to use the INSTR Function (VBA)

Description

The Microsoft Excel INSTR function returns the position of the first occurrence of a substring in a string.

Syntax

The syntax for the INSTR function in Microsoft Excel is:
InStr( [start], string, substring, [compare] )

Parameters or Arguments

start
Optional. It is the starting position for the search. If this parameter is omitted, the search will begin at position 1.
string
The string to search within.
substring
The substring that you want to find.
compare
Optional. It is the type of comparison to perform. It can be one of the following values:
VBA ConstantValueExplanation
vbUseCompareOption-1Uses option compare
vbBinaryCompare0Binary comparison
vbTextCompare1Textual comparison

Applies To

  • Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The INSTR function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel INSTR function examples and explore how to use the INSTR function in Excel VBA code:
InStr(1, "Tech on the Net", "the")
Result: 9

InStr("Tech on the Net", "the")
Result: 9

InStr(10, "Tech on the Net", "t")
Result: 15
For example:
Dim LPosition As Integer

LPosition = InStr(10, "Tech on the Net", "t")
In this example, the variable called LPosition would now contain the value 15.