EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA String and Text Functions VBA InStr
Secondary Sidebar
VBA String and Text Functions
  • VBA String and Text
    • VBA Constants
    • VBA TRIM
    • VBA Find
    • VBA Case
    • VBA Replace
    • VBA String
    • VBA Split
    • VBA InStr
    • VBA RegEx
    • VBA Left
    • VBA UCASE
    • VBA LCase
    • VBA Concatenate
    • VBA StrComp
    • VBA StrConv
    • VBA Find and Replace
    • VBA Right
    • VBA ASC
    • VBA InStrRev
    • VBA Length of String
    • VBA Format
    • VBA MID
    • VBA Concatenate Strings
    • VBA SubString
    • VBA String Comparison
    • VBA String Array
    • VBA Dynamic Array
    • VBA Replace String

VBA InStr

By Madhuri ThakurMadhuri 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:

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,

Watch our Demo Courses and Videos

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

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
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Watch our Demo Courses and Videos

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

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

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

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more