totn Excel Functions

MS Excel: How to use the INSTRREV Function (VBA)

This Excel tutorial explains how to use the Excel INSTRREV function with syntax and examples.

Description

The Microsoft Excel INSTRREV function returns the position of the first occurrence of a string in another string, starting from the end of the string. This is similar to the INSTR function which returns the position of the first occurrence, starting from the beginning of the string.

The INSTRREV function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Syntax

The syntax for the INSTRREV function in Microsoft Excel is:

InStrRev ( string, substring [, start [ , compare] ] )

Parameters or Arguments

string
The string to search within.
substring
The substring that you want to find.
start
Optional. It is the starting position for the search. If this parameter is omitted, the search will begin at position -1 which is the last character position.
compare

Optional. It is the type of comparison to perform. It can be one of the following values:

VBA Constant Value Explanation
vbUseCompareOption -1 Uses option compare
vbBinaryCompare 0 Binary comparison
vbTextCompare 1 Textual comparison

Returns

The INSTRREV function returns a numeric value.
If string2 is not found within string_being_searched, the INSTRREV function will return 0.
If string_being_searched is zero-length, the INSTRREV function will return 0.
If string_being_searched is null, the INSTRREV function will return null.
If start is null, the INSTRREV function will return #Error.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The INSTRREV function can only be used in VBA code in Microsoft Excel.

Let's look at some Excel INSTRREV function examples and explore how to use the INSTRREV function in Excel VBA code:

InStrRev ("alphabet", "a")
Result: 5

InStrRev ("alphabet", "a", -1)
Result: 5

InStrRev ("alphabet", "a", 1)
Result: 1

InStrRev ("alphabet", "a", 2)
Result: 1

InStrRev ("alphabet", "a", 3)
Result: 1

InStrRev ("alphabet", "a", 4)
Result: 1

InStrRev ("alphabet", "a", 5)
Result: 5

InStrRev ("alphabet", "a", 6)
Result: 5

InStrRev ("alphabet", "a", 7)
Result: 5

InStrRev ("alphabet", "a", 8)
Result: 5

InStrRev ("alphabet", "a", 9)
Result: 0

For example:

Dim LPosition As Integer

LPosition = InStrRev ("alphabet", "a")

In this example, the variable called LPosition would now contain the value 5.