Contents
In VBA (Visual Basic for Applications), the Right function is used to extract a specified number of characters from the right end of a string. This can be very useful when you need to retrieve a substring from a text string based on the number of characters from the end.
Basic syntax
VBA
Right(string, length)
- string: This is the string from which you want to extract the characters.
- length: This is the number of characters you want to extract from the right side of the string.
Example
VBA
Sub UseRightFunction()
Dim FullString As String
Dim ResultString As String
' Assign a value to FullString
FullString = "Hello, World!"
' Extract the last 5 characters from FullString
ResultString = Right(FullString, 5)
' Display the result in a message box
MsgBox ResultString
End Sub
When you run this code, it will display a message box with the text orld!, which are the last 5 characters of the string “Hello, World!”.
It is important to remember that if you provide a length greater than the length of the string, the Right function will simply return the entire string. If you specify a length of zero or less, the Right function will return an empty string.