The VBA Right function returns a substring from the end of a supplied string.
The syntax of the function is:
Where the function arguments are:
Str | - | The original string that you want to extract a substring from. |
Length | - | The length of the substring. |
' Example 1. Extract a substring of length 5 from the end of the string "John Michael Smith".
Dim res As String
' The variable res is now equal to the text string "Smith".
res = Right( "John Michael Smith", 5 ) |
In the example above, the VBA Right function returns the result "Smith".
' Example 2. Extract a substring of length 13 from the end of the string "John Michael Smith".
Dim res As String
' The variable res is now equal to the text string "Michael Smith".
res = Right( "John Michael Smith", 13 ) |
In the example above, the VBA Right function returns the substring "Michael Smith".
' Example 3. Extract the last part of the string "John Michael Smith", starting after the last space.
Dim pos As Integer
Dim strlen As Integer Dim res As String
pos = InStrRev( "John Michael Smith", " " )
' Now, the variables pos = 13, strLen = 18 and res = "Smith".
strLen = Len( "John Michael Smith" ) res = Right( "John Michael Smith", strLen - pos ) |
In the example above:
Further information and examples of the VBA Right function are provided on the Microsoft Developer Network.