Right, Right$ Functions
See Also Example
Returns the rightmost n characters of a string argument.
Right returns a Variant; Right$ returns a String.
The Right[$] function has these parts:
strexpr String expression from which the rightmost characters are returned. This can be any string expression. However, only Right can accept a Variant of VarType 1 (Null ) as stringexpr, in which case, a Null is returned.
n Long integer expression indicating how many characters to return. It must be between 0 and approximately 65,535, inclusive. If n is 0, the return value is a zero-length string. If n is greater than or equal to the number of characters in strexpr, the entire string is returned.
To find the number of characters in stringexpr, use Len(stringexpr).
Left, Left$ Function
Mid, Mid$ Function
Right, Right$ Function Example
The example uses the Right function to return the second of two words input by the user. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim LWord, Msg, RWord, SpcPos, UsrInp ' Declare variables.
Msg = "Enter two words separated by a space."
UsrInp = InputBox(Msg) ' Get user input.
SpcPos = InStr(1, UsrInp, " ") ' Find space.
If SpcPos Then
LWord = Left(UsrInp, SpcPos - 1) ' Get left word.
RWord = Right(UsrInp, Len(UsrInp) - SpcPos) ' Get right word.
Msg = "The first word you entered is """ & LWord
Msg = Msg & "." & """" & " The second word is """
Msg = Msg & RWord & "." & """"
Msg = "You didn't enter two words."
MsgBox Msg ' Display message.