VBA Right Function

Related Functions:
VBA Left
VBA Mid

Description

The VBA Right function returns a substring from the end of a supplied string.

The syntax of the function is:

Right( Str, Length )

Where the function arguments are:

Str-The original string that you want to extract a substring from.
Length-The length of the substring.


VBA Right Function Examples

Example 1

' Example 1. Extract a substring of length 5 from the end of the string "John Michael Smith".
Dim res As String
res = Right( "John Michael Smith", 5 )
' The variable res is now equal to the text string "Smith".

In the example above, the VBA Right function returns the result "Smith".


Example 2

' Example 2. Extract a substring of length 13 from the end of the string "John Michael Smith".
Dim res As String
res = Right( "John Michael Smith", 13 )
' The variable res is now equal to the text string "Michael Smith".

In the example above, the VBA Right function returns the substring "Michael Smith".


Example 3

' 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", " " )
strLen = Len( "John Michael Smith" )
res = Right( "John Michael Smith", strLen - pos )
' Now, the variables pos = 13, strLen = 18 and res = "Smith".

In the example above:


Further information and examples of the VBA Right function are provided on the Microsoft Developer Network.