Overview of VBA RIGHT Function:
VBA Right function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns the specified number of characters from a given string, starting from the Right most character. It has two mandatory parameters. It can be used as a Worksheet function or VBA function. The VBA Right function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA Right function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Right function, where we can use this VBA Right function and real-time examples.
Table of Contents
- Objective of Right Function
- Syntax of VBA Right Function
- Parameters or Arguments
- Where we can apply or use the VBA Right Function?
- Example 1: Extract a substring of length 5 from end of the string
- Example 2: Extract a substring of length 30 from end of the string
- Example 3: Get Last Name from Full Name using Right Function
- Example 4: Get Email extension from email using Right Function
- Instructions
- Quick View Examples & Results
Syntax of VBA Right Function
The syntax of the VBA Right function is
Right (string, [length])
Note: This Right function returns a substring (Specified number of characters) from Right side.
Parameters or Arguments
There are two mandatory parameters or arguments for the Right Function.
Where
string: The string is a mandatory argument. The string from which we want to extract a substring from right side.
Length: The length is a required argument. It represents the number of characters of the substring that you want to extract from a string.
Where we can apply or use the VBA Right Function?
We can use this VBA Right function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Extract a substring of length 5 from end of the string
Here is a simple example of the VBA Right function. This below example macro uses the Right function and extracts the Right most four characters from a given string.
'Get First Name from Full Name using Right Function Sub VBA_RIGHT_Function_Ex3() Dim sString As String Dim sLName As String Dim sPosition As Integer Dim sSubString As String sString = "F_Name L_Name" sPosition = InStr(1, sString, " ") sLName = Right(sString, Len(sString) - sPosition) MsgBox "The Last Name is :" & sLName, vbInformation, "VBA Right Function" End Sub
Output: The variable sSubString now contains the result as ‘Beautiful’. Here is the screen shot of first example output.
Example 2: Extract a substring of length 30 from end of the string
Here is another example of the VBA Right function. This below example macro uses the Right function and extracts the Right most 30 characters from a given string. There are only 17 available characters. So, it displays only 17 characters instead of 30 characters.
'Extract a substring of length 30 from starting of the string. Sub VBA_RIGHT_Function_Ex2() Dim sString As String, sSubString As String sString = "Life is Beautiful" sSubString = Right(sString, 30) MsgBox "The substring is :" & sSubString, vbInformation, "VBA Right Function" End Sub
Output: The variable sSubString now contains the result as ‘Life is Beautiful’. Here is the screen shot of second example output.
Example 3: Get Last Name from Full Name using Right Function
Here is one more example. We are extracting a last name from full name using Instr and Right function.
'Get First Name from Full Name using Right Function Sub VBA_RIGHT_Function_Ex3() Dim sString As String Dim sLName As String Dim sPosition As Integer Dim sSubString As String sString = "F_Name L_Name" sPosition = InStr(1, sString, " ") sLName = Right(sString, Len(sString) - sPosition) MsgBox "The Last Name is :" & sLName, vbInformation, "VBA Right Function" End Sub
Output: The variable sLName now contains the result as ‘L_Name’. Here is the screen shot of third example output.
Example 4: Get Email Extension from email using Right Function
Here is another example for Right function. Here getting Email extension from email using Instr and Right functions.
'Get Email extension from email using Right Function Sub VBA_RIGHT_Function_Ex4() Dim sString As String Dim sEName As String Dim sPosition As Integer Dim sSubString As String sString = "ABCD@hotmail.com" sPosition = InStr(1, sString, "@") sEName = Right(sString, Len(sString) - sPosition) MsgBox "Specified Email Extension is : " & sEName, vbInformation, "VBA Right Function" End Sub
Output: The variable sSubString now contains the result as ‘hotmail.com. Here is the screen shot of fourth example output.
Instructions
Here are the step by step instructions to use the VBA RIGHT function in Excel VBA.
- Open an Excel workbook.
- Press Alt+F11 to open VBA Editor window.
- Go to Insert menu and click on module from the available options.
- Copy above specified macros to the VBA editor.
- Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.
Quick View Examples & Results
You can find quick view of Right function examples and its results.
'VBA Right Function Quick View Examples Sub VBA_Right_Function() Dim sString As String, sSubString As String sString = "FName LName" sSubString = Right(sString, 5) 'Result: 'LName' Debug.Print sSubString sSubString = Right(sString, 6) 'Result: ' LName' Debug.Print sSubString sSubString = Right(sString, 50) 'Result: 'FName LName' Debug.Print sSubString sSubString = Right(sString, 0) 'Result: Displays Nothing Debug.Print sSubString sSubString = Right(sString, -1) 'Result: Displays Error Debug.Print sSubString End Sub
List of VBA Functions:
Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions