Excel: InStrRev Function (VBA only)
In Excel, the 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 syntax for the InstrRev function is:
InstrRev ( string_being_searched, string2 [, start [ , compare] ] )
string_being_searched is the string that will be searched.
string2 is the string to search for.
start is 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 is optional. This is the type of comparison to perform. The valid choices are:
VBA Constant Value Explanation vbUseCompareOption -1 Uses option compare. vbBinaryCompare 0 Binary comparison vbTextCompare 1 Textual comparison vbDatabaseCompare 2 Comparison based on your database.
Note:
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 2007, Excel 2003, Excel XP, Excel 2000
For example:
InstrRev ("alphabet", "a") would return 5. InstrRev ("alphabet", "a", -1) would return 5. InstrRev ("alphabet", "a", 1) would return 1. InstrRev ("alphabet", "a", 2) would return 1. InstrRev ("alphabet", "a", 3) would return 1. InstrRev ("alphabet", "a", 4) would return 1. InstrRev ("alphabet", "a", 5) would return 5. InstrRev ("alphabet", "a", 6) would return 5. InstrRev ("alphabet", "a", 7) would return 5. InstrRev ("alphabet", "a", 8) would return 5. InstrRev ("alphabet", "a", 9) would return 0.
VBA Code
The InstrRev function can only be used in VBA code. For example:
Dim LPosition As Integer
LPosition = InstrRev ("alphabet", "a")
In this example, the variable called LPosition would now contain the value 5.
