VBA INSTR – Find Text in a String

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. 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!

alt text

Learn More!

<<Return to VBA Examples

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

One Comment

  1. Sunny September 4, 2017 at 3:27 am - Reply

    Sorry, it doesn’t return the number of times but the position. Is there any fix? Thanks

Leave A Comment