You are reading the article How To Use Vba Right Function In Excel? updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How To Use Vba Right Function In Excel?
VBA Right Function
VBA Right is categorized under text or String function, useful to chop characters from a string.
It can be used as either procedure or function in a vba editor window.
It is frequently used to extract a substring from a full-text string, i.e., either to pull out the last name from a full name or to extract email extension from email id.
VBA Right function allows you to extract the right-side part of the string from a full-text string.
VBA string functions do not change the original string. Usually, they will return a new string based on the function you input in the code.
VBA RIGHT function returns a substring from within a supplied text or a string i.e. starting with the right-most character (from the end of the string).
Syntax of Right in Excel VBAThe syntax for the VBA Right function in excel is as follows:
Watch our Demo Courses and Videos
Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.
Right(Str,Length)
Str: It is a text String from where you need to extract the specified number of characters.
Length: Number of characters to extract or to return from starting from extreme rightmost character or position, it should be the numeric expression which indicates how many characters to return.
Note: For length argument, If it is mentioned 0, it will return a zero-length string (“”). If the length argument entered is equal to the number of characters of string or if it is greater than the number of characters of string then it will result in or return an entire string. If length is Null or less than 0, it will return an error message
How to Use VBA Right Function in Excel?
You can download this VBA Right Excel Template here – VBA Right Excel Template
Example #1 – VBA RightStep 2: Insert a module from the insert tab to start writing codes in VBA.
Now the blank module is created, it is also called a code window, where you can start writing VBA RIGHT function or procedure codes.
Example #2 – VBA Right Function CodeStep 1: Suppose, I have the word “THURSDAY” and I want to extract the right side word i.e. “DAY” from this sentence with the help of VB RIGHT function macro code.
Step 1: In the VBA editor, I have a given a name as VBA_R() after typing Sub.
Code:
Sub
VBA_R()End Sub
Step 2: As VBA RIGHT function is categorized under string type Variables, DIM (Dimension) is used in a VBA code to declare a variable name, it’s typing.
In VBA, you need to always declare initially that you are setting up a variable. This is done (mostly) with the word Dim. DIM is used to declare variable & storage allocation for the variable.
Code:
Sub
VBA_R()Dim
rght AsString
End Sub
Step 3: After declaring a variable, Assign a value to this variable with the help of the RIGHT function.
Code:
Sub
VBA_R()Dim
rghtAs String
rght = right(End Sub
First parameter or argument is String i.e. It is a text String from where you need to extract the specified number of characters. Here it is “THURSDAY”
Note: When using strings argument, you have to insert the text value in quotation mark.
Step 4: Length as long: Number of characters to extract or to return from starting from extreme rightmost character or position, it should be the numeric expression which indicates how many characters to return, here I want the word “DAY” so the number of characters is 3.
Code:
Sub
VBA_R()Dim
rghtAs String
rght = right("THURSDAY", 3) MsgBox rghtEnd Sub
Example #3 – VBA RightNow, instead of output appearing in the message box, I want the result or output data to appear in the worksheet.
In the worksheet, I have a data, i.e. USA city & state (in abbreviated form) in the cell “A4”, now I want to extract state (in abbreviated form), Here I can use Right function with slight modification in the code. Let’s check out, how it can be done.
Step 1: After declaring a variable, I have to input the variable name again and cell address of the full-text string with the help of Range or cell function.
Sub
VBA_R2()Dim
rghtAs String
rght = Range("a4")End Sub
Step 2: Now, again I need to enter the variable name, and apply RIGHT function & input its arguments.
Code:
Sub
VBA_R2()Dim
rghtAs String
rght = Range("a4") rght = right("Carlsbad, CA", 2)End Sub
Step 3: In the previous example, I entered the msg box for the result to be displayed, now, I want the result to appear in a specific cell in a worksheet. for this, I need to enter range or cell function for the result to be displayed.
Code:
Sub
VBA_R2()Dim
rghtAs String
rght = Range("a4") rght = right("Carlsbad, CA", 2) Range(End Sub
Step 4: Now, let’s apply range function, initially I need to input the range or cell function and Later enter the variable name, so that in that specific cell (“B4”), the result appears.
Code:
Sub
VBA_R2()Dim
rghtAs String
rght = Range("a4") rght = right("Carlsbad, CA", 2) Range("B4").Value = rghtEnd Sub
Thinks to Remember
Most of the cases, for better performance different version of the Right function, is used i.e. Right$ instead of the default Right is used. This is because the Right function returns a variant instead of a string as therefore it impacts better performance in the result
RIGHTB i.e. Right function with byte data: In this function, length argument specifies the number of bytes to return
Both RIGHT$ & RIGHTB$ function is usually used to return a byte data from a String data type instead of Variant/String data type
Recommended ArticlesThis is a guide to VBA Right. Here we discuss how to use Excel VBA Right function along with practical examples and downloadable excel template. You can also go through our other suggested articles –
You're reading How To Use Vba Right Function In Excel?
Update the detailed information about How To Use Vba Right Function In Excel? on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!