INSTR Function

You can use the Instr Function in VBA to test if a string contains certain text. The result is the number of times the specified text appears in the string.

The following code snippet tests the string “Look in this string” for the word “Look”. The result is 1 since the word appears once:

This test is case-sensitive. For a case-insensitive test, look at the example below.

Case-Insensitive INSTR Test

To skip the case sensitive check, enclose the look-in string and test string in the Ucase Function.

Example:This returns 0 because the case is wrong for the word “look”

However the following returns 1 because it turns both strings to all caps before evaluating:

If In String

A quick use of the INSTR Function in an if…then statement

VBA Coding Made Easy

Stop searching for VBA code online. Download our free add-in to quickly insert any of the sample code found on our site into the Visual Basic Editor.

alt text

Stop searching for VBA code online. Try the Code VBA Add-in…

Free Download!

<<Return to VBA Examples

Did you find this VBA tutorial useful? Then share it with your friends and colleagues: