Learn about VBA Right Functions

0
67

Learn about VBA Right Function

The Right function in VBA (Visual Basic for Applications) is used to extract a specified number of characters from the right side of a string. It is commonly used in Excel VBA for string manipulation tasks.

Syntax

Right(string, length)

string: The text or string from which you want to extract characters.

length: The number of characters you want to extract from the right side of the string

Returns

The Right function returns a string containing the specified number of characters from the end (right side) of the input string.

Examples

Basic Use VBA Code for VBA Right Function

Example

VBA Code:

Sub example()

Dim mystring As String

Dim result As String

mystring = "Hello, World"

result = Right(mystring, 5)

'extract "world"

MsgBox result

End Sub

Handling Numeric Value in VBA Right Function

VBA Code:

Sub example2()

Dim mystring As String

Dim result As String

mystring = "123456"

result = Right(mystring, 4)

MsgBox result

'result will be 3456




End Sub

 

Working with Cell Value in VBA Right Function

VBA Code:

Sub example3()

Dim cellvalue As String

Dim result As String

cellvalue = ActiveSheet.Range("A1").Value

result = Right(cellvalue, 3)

MsgBox result

End Sub

Notes

If length is greater than or equal to the string’s length, the entire string is returned.

VBA Code:

Sub example4()

Dim mystring As String

Dim result As String

mystring = “Hello”

result=Right(mystring, 10) ' Returns "Hello"

Msgbox result

End Sub

If length is 0, an empty string (“”) is returned.

Right(“Hello”, 0) ‘ Returns “”

The Right function works only with strings. If a numeric value is passed, it will be implicitly converted to a string.

Excel File for Download

Other related topics

  1. VBA String Functions : Excel VBA provides a variety of string functions that help manipulate and analyze text. Here are the commonly used VBA string functions with examples
  2.  VBA VLookup : In VBA, you can use the VLookup function to search for a value in the first column of a table and return a value in the same row from another column.
  3. VBA LEFT Function : The Left function in VBA is used to extract a specified number of characters from the start (left side) of a string
  4.  VBA Split Function:
  5. VBA TRIM Function 

LEAVE A REPLY

Please enter your comment!
Please enter your name here