VBA – Right String
The Right function in VBA extracts a specified number of characters from the right side of a given string.
Syntax:
Right(string, length)
Parameter:
Return Value:
Example: Extract the last 3 characters
Dim str1 As String
Dim rightStr As String
str1 = "Hello, World!"
rightStr = Right(str1, 3)
' rightStr will be "d!"
Extract more characters than the string length:
Dim str1 As String
Dim rightStr As String
str1 = "Hello, World!"
rightStr = Right(str1, 15)
' rightStr will be "Hello, World!" (Returns the entire string)
Use Cases:
Key Considerations:
length is 0, an empty string (“”) is returned.length is greater than the length of the string, the entire string is returned.