EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA String and Text Functions VBA InStr
 

VBA InStr

Madhuri Thakur
Article byMadhuri Thakur

VBA InStr Function

VBA InStr Function

In VBA we have a function called InStr Function which is used to return the position of the first occurrence of a substring in a string. Which means, for example, we have a name called “Jhon” and we have to find the occurrence of the position of H alphabet in the string. This is what the function is used for.

 

 

Formula for InStr Function in Excel VBA

Excel VBA InStr function has the following syntax:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of InStr

In the syntax Start and compare are optional while string and substring values are required. A string is a string where we want to find our substring and substring is the value which is required to be found in the string. Suppose if we had to use the above syntax on our example of Jhon above the VBA code would be as below,

Instr(“Jhon”,”H”) and the result would be 2 as the position of alphabet H is 2 in the string.

Now we move towards the optional parts of the syntax which are, start and compare. The start is the position of the string from where we want to start the search, whereas compare is of three types,

  1. VbUseCompareOption: (-1) It is used to do option compare.
  2. VbBinaryCompare: (0) It is used to do binary compare.
  3. VbTextCompare: (1) It is used to the textual comparison.

But we need a variable to store the value of the result. As the value is an integer so the variable defined should also be an integer. Now if we have calculated the value we also need to display the result we use msgbox function to display the result.

How to Use Excel VBA InStr Function?

We will learn how to use a VBA InStr Excel function with few examples.

You can download this VBA InStr Excel Template here – VBA InStr Excel Template

VBA InStr Function – Example #1

In the explanation above I used the word Jhon as a string and wanted to find the position of H in the string. I will write the code for the same in VBA and use Instr function to find the position of J in the string.

Follow the below steps to use InStr function in VBA.

Step 1: In the Developer Tab click on Visual Basic to open the VB Editor.

Example 1-1

Step 2: Once the module is open, declare a sub-function to start writing the code.

Code:

Sub Compare()

End Sub

VBA Instr Example 1-2

Step 3: Now the function is declared, We need to declare a variable to store the position of the substring.

Code:

Sub Compare()

Dim Pos As Integer

End Sub

VBA Instr Example 1-3

Step 4: Now we write the InStr function to find out the position of our substring.

Code:

Sub Compare()

Dim Pos As Integer 
Pos = InStr("Jhon", "J")

End Sub

VBA Instr Example 1-4

Step 5: To return the position of the substring as a value, we use the MsgBox function and close the subfunction.

Code:

Sub Compare()

Dim Pos As Integer 
Pos = InStr("Jhon", "J")

MsgBox Pos

End Sub

VBA Instr Example 1-5

Step 6: If we run the code it shows the position of alphabet J in the string.

Result of Example 1-6

Step 7: If we change the substring to check that the code is running properly, let us find the alphabet n in the string.

Code:

Sub Compare()

Dim Pos As Integer 
Pos = InStr("Jhon", "n")

MsgBox Pos

End Sub

VBA Instr Example 1-7

Step 8: Now if we run the code we can see the position of alphabet n in the string.

Result of Example 1-8

VBA InStr Function – Example #2

Now we have seen what the required arguments of the function do, now let us use the optional ones. The String is “I am a Good Boy but not a Good Runner”. We will find the substring “Good” from the string but ignore the first Good from the sentence.

Follow the below steps to use InStr function in VBA.

Step 1: Open the VB editor from the developers’ tab by clicking on Visual Basic.

Step 2: Start by declaring a sub-function.

Code:

Sub Compare1()

End Sub

VBA Instr Example 2-1

Step 3: Now declare a variable to store the position of the substring.

Code:

Sub Compare1()

Dim Pos1 As Integer

End Sub

VBA Instr Example 2-2

Step 4: Now we will use InStr function to find the position of the string.

Code:

Sub Compare1()

Dim Pos1 As Integer 
Pos = InStr(12, "I am a Good Boy but bot a Good Runner", "Good", vbBinaryCompare)

End Sub

VBA Instr Example 2-3

Step 5: Now we use the Msgbox function to return the value of the string.

Code:

Sub Compare1()

Dim Pos1 As Integer 
Pos = InStr(12, "I am a Good Boy but bot a Good Runner", "Good", vbBinaryCompare)

MsgBox Pos

End Sub

VBA Instr Example 2-4

Step 6: Now if we run the code we can get the position of the substring.

Result of Example 2-5

Explanation of Excel VBA Instr Function

InStr function in VBA is used to return the position of the value of the substring in a string. What happens if the value is not found? If the value is not found in the string the result displayed is zero.

One thing we should always remember the Instr function is case sensitive. To prove this we move to example 1 where we used instr function to search an alphabet.

VBA Instr 1

What happens if the substring is “N” instead of “n’? let us find out. I have changed the case of the alphabet in the string.

VBA Instr 2

Now if I run the code I will get the result as shown below.

Result

This proves that the instr function is case sensitive.

Things to Remember

  • The two required fields are String and Substring.
  • Instr Function is case sensitive.
  • The value of the InStr function is an integer.

Recommended Articles

This has been a guide to VBA InStr Function. Here we discussed how to use Excel VBA InStr Function along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA VLOOKUP
  2. VBA RGB
  3. VBA On Error
  4. VBA Login

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA InStr Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW