VBA Right Function

The VBA RIGHT function derives characters from the right side of the text values you give it. Excel has a lot of text functions to help us deal with data that is in text form. The LEN, LEFT, RIGHT, and MID functions are great ways to get characters out of text values. One common way to use these functions is to separately get the first and last names from the full terms. The VBA RIGHT function takes a string and gives back a string-based result. If the string argument given to this function is Null, it will return Null. As its name suggests, the VBA RIGHT function takes only the rightmost characters from the String. The space can be found when the RIGHT function is used with the InStr function. It makes it easy for the user to tell which word is in the given String. The worksheet also has the RIGHT formula. The VBA RIGHT function can only be accessed through the worksheet function class. So, VBA also has the built-in RIGHT function.

Syntax

RIGHT (String, Length)

Parameter

String (required): The length of the String to be extracted is represented by the String argument.

Length (required): The length argument tells you the number of characters from the right portion of the String you want to get.

Example

Find the last name from the string “Hello World”.

The VBA RIGHT function is one of the built-in functions frequently used by Excel users to retrieve data from the right side of a String or user-supplied value.

Here are the steps for writing a VBA macro to use the VBA RIGHT function to get the first name from the given String:

Step 1: Go to File > New > VBA Developer. Go to the Excel worksheet, click on the ribbon tab, then on the developer window and visual basic editor, or press Alt+F11.

vba-editor_3.PNG

Step 2: You will see the VB Editor window. The following measure is to do a module. Select Module from the Insert menu in the ribbon.

VBA Module

Step 3: You will see that VBA adds a new window for the Module. Initiate the programme by declaring the variable and introducing your macro name.

Sub VBA_RIGHT_Function()

Dim str_Right_String As String

End Sub

Step 4: After that, we’ll add the RIGHT function. In the arguments, we will pass the String. In the length argument, we will give 5 since we want to get the first five characters from the right segment of the String.

Sub VBA_RIGHT_Function()

Dim str_Right_String As String

str_Right_String = "Hello World"

End Sub

Step 5: Put the obtained value in the variable, and then use MsgBox to return the variable.

Sub VBA_RIGHT_Function()

Dim str_Right_String As String

str_Right_String = "Hello World"

MsgBox Right(str_Right_String, 5)

End Sub

Output

Either click Run or press the F5 key to run your macro. You’ll see that VBA launches a MsgBox showing the first five characters of your String from the right.

VBA Right Function Output

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top