Return to VBA Code Examples

VBA InStrRev Function – Find Text From Right

This tutorial will demonstrate how to use the InStrRev VBA function to find text from the right..

InStrRev Function

The VBA InStrRev function searches for the position of a substring inside a string. It returns the position counting from left to right but it performs the search from the end of the string to the beginning.

VBA InStrRev Find Substring

The VBA InStrRev function can be used the find the position of a substring inside a string.

VBA InStrRev Starting Position

VBA InStrRev Function can start the search for a substring from an assigned position counted from left to right.

VBA InStrRev Case Sensitivity

VBA InStrRev Function by default is case sensitive. You can change that behavior to case insensitive using an optional parameter. In that case, you must also define the starting position of the search.

VBA InStrRev Last Space

VBA InStrRev Function can find the position of the last space in a string.

VBA InStrRev Second to Last Space

VBA InStrRev function can find the position of the second to last space in a string. We can use VBA InStrRev function to find the last space in a string and then we can use again InStrRev with a new starting point to find the position of the second to last space

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

VBA InStrRev Return Filename

VBA InStrRev function can be used to easily return the filename from a string that contains the path and the filename. We will use InStrRev function with functions VBA Len and VBA Right.

VBA Len function returns the length of a string:

VBA Right function returns n characters from the end of a string:

We can use InStrRev function to find the last occurrence of “\” in the pathname and use Len function to calculate the length of the filename. Right can then extract the filename.