How to Use VBA InStrRev Function (7 Suitable Examples)

When you need to find the position of a character inside a string or the occurrence of a substring inside a string, the VBA InStrRev function might be the best option. Unlike the InStr function, the InStrRev function starts searching from the end of the string. In this article, we will illustrate the ins and outs of the InStrRev function using 7 examples.

The following is an overview of the InStrRev function.

Overview of VBA InstRev Function


Download Practice Workbook


Introduction to the VBA InStrRev Function in Excel

InStrRev, which refers to “In String Reverse”, is a VBA function to get the position of a substring within a given string.

  • Function Objective:

Finds out the position of a string that occurs within another, starting from the beginning.

Formula of VBA InstRev

  • Syntax:

InStrRev(StringCheck, StringMatch, [ Start, [ Compare ]])

  • Arguments:

The InStrRev function has four arguments:

Argument Required/Optional Explanation
StringCheck Required The string from where you want to get the position
StringMatch Optional The substring that you wish to get
Start Optional Refers to starting position. The default value of this argument is -1 (starting from right to left); if you omit the value.
Compare Optional Represents three kinds of comparisons as shown in the following table. Excel takes vbBinaryCompare (value:0) if you skip the value.

Three types of comparisons of the Compare argument are-

VBA Constant Value Description
vbUseCompareOption -1 Uses option compare
vbBinaryCompare 0 Binary comparison
vbTextCompare 1 Textual comparison
  • Return Value:

Returns a numeric value.


Examples of Using VBA InStrRev Function in Excel

In the first 6 examples, you’ll see the uses of the InStrRev function for a single string i.e. “Miles to Go Before I Sleep”.


Example 1 – VBA InStrRev to Find out the Position of a Substring 

We’ll use the function to find out the position of a substring “M” inside the string “Miles to Go Before I Sleep”.

Step 1: 

Open a module by clicking Developer > Visual Basic.

How to Insert VBA Code

Go to Insert>Module.

How to Insert VBA Code

Step 2: 

Enter the following code in your module.

Sub Finding_The_Position_Substring()
Dim A As Integer
A = InStrRev("Miles to Go Before I Sleep", "M")
MsgBox A
End Sub

VBA InstRev to Find out The Position of A Substring

Step 3:

Run the code (F5 is the keyboard shortcut).

You’ll see the output is 1.

VBA InstRev to Find out The Position of A Substring

Note: Sometimes the StringCheck is called simply a “string” and the StringMatch is also called a “substring”.

Read More: How to Call a Sub in VBA in Excel (4 Examples)


Example 2 – VBA InStrRev to Get the Position of a Substring with Given Starting Position 

In the previous example, we skipped the value of the Start argument. What if we put 10 as a starting position for the substring “e”?.

Enter the following code into the module.

Sub Getting_Position_Substring_Given_Starting_Position()
Dim A As Integer
A = InStrRev("Miles to Go Before I Sleep", "e", 10)
MsgBox A
End Sub

Get The Position of A Substring with Given Starting Position

Run the code, and you’ll get the output 4.

Get The Position of A Substring with Given Starting Position

Related Content: How to Use Fix Function in Excel VBA (4 Examples)


Example 3 – VBA InStrRev to Extract the Position in Case Sensitivity

We’ll now examine whether the InStrRev function is case sensitive or not.

That means, if we put the substring as “g” and run the code, what will be the output.

After running the code, if we get 0, it means that the function is not case sensitive.

Enter the following code into a new module.

Sub Extracting_The_Position_Case_Sensitivity()
Dim A As Integer
A = InStrRev("Miles to Go Before I Sleep", "g")
MsgBox A
End Sub

VBA InstRev to Extract The Position in Case Sensitivity

Run the code, and the output will look as follows.

Extract The Position in Case Sensitivity

As the output is 0, it is clear that the InStrRev function is not case-sensitive.

In the above code, we skipped the value of the Compare argument, so Excel takes vbBinaryCompare as the default value.

If you input the argument as vbTextCompare, you’ll see a different output for the same string and substring.

The adjusted code will be as follows.

Sub Extracting_The_Position_Case_Sensitivity()
Dim A As Integer
A = InStrRev("Miles to Go Before I Sleep", "g", , vbTextCompare)
MsgBox A
End Sub

Extract The Position in Case Sensitivity

After running the code, you’ll get the output 10 (not 0), which means the function is case sensitive in this case.

VBA InstRev to Extract The Position in Case Sensitivity

The case sensitivity of the InStrRev function depends on using  the Compare argument.

Read More: How to Use InStr Function in VBA (3 Examples)


Example 4 – VBA InStrRev to Retrieve the Position of Last Space

Enter the following code in a new module.

Sub Retrieve_Position_Last_Space()
Dim A As Integer
A = InStrRev("Miles to Go Before I Sleep", " ")
MsgBox A
End Sub

Retrieve the Position of Last Space

Run the code, and you’ll get the following output.

Retrieve the Position of Last Space

Related Content: How to Use VBA Replace Function in Excel 


Example 5 – VBA InStrRev to Get the Position of Second to Last Space

To find the position of second to the last space using the InStrRev function, you have to utilize the function two times.

The first instance of the InStrRev function returns the position of the last space and the InStrRev function returns the position of second to the last space.

Enter the following code into a new module.

Sub Getting_Position_Second_to_Last_Space()
Dim LastPos As Integer
Dim SecondLastPos As Integer

LastPos = InStrRev("Miles to Go Before I Sleep", " ")
SecondLastPos = InStrRev("Miles to Go Before I Sleep", " ", LastPos - 1)

MsgBox SecondLastPos
End Sub

VBA InstRev to Get The Position of Second to Last Space

Run the code and the output will look as follows.

VBA InstRev to Get The Position of Second to Last Space

Read More: [Fixed!] Excel VBA InStrRev Not Working


Example 6 – VBA InStrRev to Find out the Position of “ZZ” within the String

In this example, we’ll see the process of finding out the position of “ZZ” within the string. “ZZ” is a substring (StringMatch) that is not available in the string.

Enter the following code into a new module.

Sub Finding_The_Position_ZZ()
Dim A As Integer
A = InStrRev("Miles to Go Before I Sleep", "ZZ")
MsgBox A
End Sub

VBA InstRev to Find out The Position of “ZZ” within The String

Run the code and the output is 0 as shown in the following image.

VBA InstRev to Find out The Position of “ZZ” within The String

Read More: How to Use VBA StrComp in Excel 


Example 7 – VBA InStrRev to Retrieve the Position of Last Space in Case of a Dataset

In most cases, we use a dataset in Excel and want to find the output for the dataset.

Assuming that in our dataset we have four strings (starting from B5 cell) and we want to find the position of the last space for each string.

Retrieve the Position of Last Space in Case of A Dataset

Enter the following code into a new module.

Sub Dataset_Example_InStrRev()
Range("D5").Value = InStrRev(Range("B5"), " ")
Range("D6").Value = InStrRev(Range("B6"), " ")
Range("D7").Value = InStrRev(Range("B7"), " ")
Range("D8").Value = InStrRev(Range("B8"), " ")
End Sub

Retrieve the Position of Last Space in Case of A Dataset

Run the code and the output will be as follows.

Retrieve the Position of Last Space in Case of A Dataset


Things to Remember

1. The InStrRev function finds 0 if the string match or substring is not found.

2. It returns Null if the string check or string match is Null.

3. If the value of the Start argument is larger than the length of the string check, the function returns 0.

4. The InStrRev and InStr functions are not the same in VBA.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Md. Abdul Kader
Md. Abdul Kader

MD. ABDUL KADER is an engineer with a talent for Excel and a passion for VBA programming. To him, programming is like a time-saving wizard, making data manipulation, file handling, and internet interactions a breeze. His skill set extends to SWM, GIS, RS, and SPSS. He holds a B.Sc in Urban & Regional Planning from Chittagong University of Engineering and Technology and has shifted to become a content developer. In this role, he crafts technical content centred around... Read Full Bio

2 Comments
  1. I don’t understand what “InstRev” is. The article repeatedly uses “InstRev”. Every time the article shows Excel VBA the function shown is NOT “InstRev”. Excel VBA uses “InStrRev”. The difference if not obvious is Excel VBA requires an additional ‘r’ that is not included in “InstRev” that is used throughout the article.

    Article = InstRev
    VBA = InStrRev

    If in fact ‘InstRev’ is the exact same as ‘InStrRev’ it would be helpful to explain as much to prevent confusion I would think.
    If it is a typo it is certainly hurting your page views as the URL includes instrev instead of instrrev.

    • Thank you so much, Adam. We are really grateful to you for pointing it out. The article is updated.

      Thanks & Regards
      Md. Shamim Reza (ExcelDemy Team)

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo