Instrrev function is to get the position of the first occurrence
of one string within another. The search String is from right to left
inside another String to get the position for example we wish to get the
position of back slash "\" from path
"C:\Users\UserName\Desktop\MyBook.xlsx", if start from -1 then the
position of back slash is 26 but if we start from 5 then the position is 3.
Constant | Value | Description |
vbUseCompareOption | -1 | Performs a comparison by using the setting of the Option Compare statement. |
vbBinaryCompare | 0 | Performs a binary comparison. |
vbTextCompare | 1 | Performs a textual comparison. |
vbDatabaseCompare | 2 | Microsoft Access only. Performs a comparison based on information in your database. |
Normally for Compare Value we use 0 (case sensitive and default) or 1.Usually we omitted this value to let default which is equal to 0 (binary comparison). For more clear explanation refer below picture.
VBA Vode: (Example to get Postion of Back slash \)
Sub Examples_InStrrev()
Dim StrLink As String
StrLink = "C:\Users\UserName\Desktop\MyBook.xlsx"
'To Get MyBook.xlsx from above string
Debug.Print Right(StrLink, Len(StrLink) - InStrRev(StrLink, "\", -1, 1))
'To Get xlsx from above string
Debug.Print Right(StrLink, Len(StrLink) - InStrRev(StrLink, ".", -1, 1))
End Sub
VBA Vode:
Sub Examples_InStrrevTest()
Dim StrLink As String
Dim iPos As Long
StrLink = "C:\Users\UserName\Desktop\MyBook.xlsx"
iPos = InStrRev(StrLink, "Name", -1)
If iPos = 0 Then
MsgBox "Keyword Name not found."
Else
MsgBox "Keyword Name found Start at= " & iPos
End If
End Sub
Note:
Usually we use InStrrev Function combine with other functions like Left,Right,Mid,Len and etc.
excel training online, visual basic for applications at below links.
Microsoft Reference-Instrrev-function
Other Reference-Instrrev-function.htm
Leave your comments if you have any request.
Practice makes perfect.
Thank You.
No comments:
Post a Comment