VBA -InString Reverse



The InStrRev function returns the first occurrence of one string within another string. The Search happens from the right to the left.

Syntax

InStrRev(string1,string2[,start,[compare]])

Parameter Description

  • String1 − A required parameter. String to be searched.

  • String2 − A required parameter. String against which String1 is searched.

  • Start − An optional parameter. Specifies the starting position for the search. The search begins at the first position from the right to the left.

  • Compare − An optional parameter. Specifies the string comparison to be used. It can take the following mentioned values.

    • 0 = vbBinaryCompare - Performs Binary Comparison (Default)

    • 1 = vbTextCompare - Performs Text Comparison

Example

Add a button and place the following function.

Private Sub Constant_demo_Click()
   var = "Microsoft VBScript"
   msgbox("Line 1 : " & InStrRev(var,"s",10))
   msgbox("Line 2 : " & InStrRev(var,"s",7))
   msgbox("Line 3 : " & InStrRev(var,"f",-1,1))
   msgbox("Line 4 : " & InStrRev(var,"t",5))
   msgbox("Line 5 : " & InStrRev(var,"i",7))
   msgbox("Line 6 : " & InStrRev(var,"i",7))
   msgbox("Line 7 : " & InStrRev(var,"VB",1))
End Sub

Upon executing the above script, it produces the following result.

Line 1 : 6
Line 2 : 6
Line 3 : 8
Line 4 : 0
Line 5 : 2
Line 6 : 2
Line 7 : 0 
vba_strings.htm
Advertisements