How to use the VBA INSTRREV Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA INSTRREV Function (Syntax + Example)

The VBA INSTRREV function is listed under the text category of VBA functions. When you use it in a VBA code, it returns the starting position number of a sub-string (specified by you) from another string. Unlike INSTR, it starts searching for the sub-string from the right side of the string, which means the end to start.

Syntax

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

Arguments

  • StringCheck: The string in which you want to search.
  • StringMatch: The string that you want to search for.
  • [Start]: An integer to specify the position from where you want to start the search [This is an optional argument and if omitted VBA takes 1 by default].
  • [Compare]: A string value to define the comparison to make while filtering array [This is an optional argument and if omitted VBA takes vbBinaryCompare by default].
    • vbBinaryCompare: For binary comparison.
    • vbTextCompare: For text comparison.
    • vbDatabaseCompare: For Database Comparison. 

Example

To practically understand how to use VBA INSTRREV function, you need to go through the below example where we have written a vba code by using it:

Sub example_INSTRREV()
Range("B1").Value = InStrRev(Range("A1"), " ")
End Sub

In the above code, we have used the INSTRREV to find the space within the value (Excel Champs) in cell A1 and it has returned 6 as a result, there’s the first space is between “Excel” and “Champs” whose position is 6.

Notes

  • If “stringcheck” or “stringmatch” is NULL then it will return NULL in the result.
  • If “stringmatch” cannot be found in “stringcheck” then it will 0 is the result.

Related Functions

FORMAT | INSTR | LCASE | LEFT | LEN | LTRIM | MID | REPLACE | RIGHT | RTRIM | SPACE | STRCOMP | STRCONV | STRING | STRREVERSE | TRIM | UCASE