Return to VBA Code Examples

VBA INSTR – Find Text in a String

INSTR Function

The VBA Instr Function checks if a string of text is found in another string of text.  It returns 0 if the text is not found. Otherwise it returns the character position where the text is found.

The Instr Function performs exact matches. The VBA Like Operator can be used instead to perform inexact matches / pattern matching by using Wildcards.

Instr Example

The following code snippet searches the string “Look in this string” for the word “Look”. The Instr Function returns 1 because the text is found in the first position.

This second example returns 7 because the text is found starting in the 7th position:

Important! The Instr Function is case-sensitive by default. This means “look” will not match with “Look”. To make the test case-insensitive read below.

Instr Syntax

The syntax for the Instr function is as follows:

[start] (optional) – This optional argument is the starting position of the search. Enter 1 to start searching from position 1 (or leave blank). Enter 5 to start searching from position 5. Important! The INSTR function calculates the character position by counting from 1 NOT from the [start] position.

string – The string of text to search in.

substring – The string of text to find in the primary string.

[compare] (optional) – By default, Instr is case-sensitive. By setting this argument you can make Instr Case insensitive:

Argument vb Value

Argument Integer Description


(Default) Case-sensitive



Not Case-sensitive



MS Access Only. Uses information in the database to perform comparison.

Instr Start Position

The Instr start position allows you to indicate the character position where you will begin your search.  Keep in mind however, the Instr output will always count from 1.

Here we set the start position to 3 to skip the first B:

The result is 6 because the second B is the 6th character in the string.

Case-Insensitive INSTR Test

By default, VBA treats “L” different from “l”. In other words, VBA is case-sensitive. This is true of all text functions.  To make VBA case-insensitive, set the [compare] argument to 1 or vbTextCompare.

Alternatively, you can add Option Compare Text to the top of your code module:


Option Compare Text will impact all of the code in that module. I personally place this at the top of any module that deals with text because I never care about case differences.

InstrRev Function

The Instr Function searches from the left. Instead you can search from the right using the InstrRev Function.  The InstrRev Function works very similarly to the Instr function.

Just like the Instr function this will return 1 because there is only one instance of “Look” in the text. But if we add a second “Look”, you’ll see that it returns the position of the right-most “Look”:


Next we will review more Instr examples.

Tired of Searching for VBA Code Examples? Try AutoMacro!

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

VBA Instr


Learn More!

InString Examples

If String Contains Substring

Here we will use an If statement to test if a string contains a a substring of text:

Find Text String in a Cell

You can also find a string in a cell:

vba find text in cell

Or loop through a range of cells to test if the cells contain some text:

search range for text


Great Product. AutoMacro doesn't just write your code, it teaches as you go!" - Tony, UK

Learn more

Read our 900+ Reviews

Find Position of a Character in a String

This code will find the position of a single character in a string and assign the position to a variable:


VBA Programming | Code Generator does work for you!

Search String for Word

This code will search a string for a word:


If Variable Contains String

This code will test if a string variable contains a string of text:


Instr and the Left Function

Instr can be used along with other text functions like Left, Right, Len, and Mid to trim text.

With the Left function you can output the text prior to a string of text:

Using Instr in Microsoft Access VBA

All of the above examples work exactly the same in Access VBA as in Excel VBA.

vba instr access

To learn more, read our article: VBA text functions

<<Return to VBA Examples