VBA Right Function - Extract Text From Right - Automate Excel
Maybe your like
In this Article
- Right Function
- VBA Right function Last n Characters
- VBA Right Function Last n Characters in a Variable
- VBA Right Function Last n Characters from a Cell
- VBA Right Function Trim off First Letter
- VBA Right to Extract Text after Space
- VBA Right to Extract Last Word
This tutorial will demonstrate how to use the Right VBA function to extract text from the right.
Right Function
VBA Right function Last n Characters
The VBA Right function returns the last n characters from a string:
Sub RightExample_1() MsgBox Right("ABCDEFGHI", 4) 'Result is: "FGHI" MsgBox Right("ABCDEFGHI", 2) 'Result is: "HI" MsgBox Right("ABCDEFGHI", 1) 'Result is: "I" MsgBox Right("ABCDEFGHI", 100) 'Result is: "ABCDEFGHI" End SubVBA Right Function Last n Characters in a Variable
As shown above, you can define a string simply by entering text surrounded by quotation marks. But the RIGHT Function will also work with string variables. These examples will extract the last n characters from a string variable.
Sub RightExample_2() Dim StrEx As String 'Define a string variable StrEx = "ABCDEFGHI" MsgBox Right(StrEx, 4) 'Result is: "FGHI" MsgBox Right(StrEx, 2) 'Result is: "HI" MsgBox Right(StrEx, 1) 'Result is: "I" MsgBox Right(StrEx, 100) 'Result is: "ABCDEFGHI" End SubVBA Right Function Last n Characters from a Cell
Strings can be defined in VBA code but also you can use values from cells. Read the value of a cell, keep it in a string variable, and extract last n characters from that Worksheet Cell value.

VBA Right Function Trim off First Letter
To remove letters from the beginning of a string, use the RIGHT Function along with the LEN Function.
The VBA LEN function counts the number of characters in a string:
Len(StrEx)By combining the functions, we can remove a certain number of characters from the start of the string:
Sub RightExample_4() Dim StrEx As String 'Define a string variable StrEx = "ABCDEF" MsgBox Right(StrEx, Len(StrEx)) 'Result is: "ABCDEF" MsgBox Right(StrEx, Len(StrEx) - 1) 'Result is: "BCDEF" MsgBox Right(StrEx, Len(StrEx) - 2) 'Result is: "CDEF" End SubVBA Right to Extract Text after Space
To extract the last name from a string with a full name, use the Right Function along with Len and Instr Function.
The VBA LEN function counts the number of characters in a string:
Len(StrEx)The VBA InStr function searches for a substring inside a string and returns the position number of the substring.
InStr(StrEx, " ")By combining the functions, we can extract the part after the first space from a phrase:
Sub RightExample_5() Dim StrEx As String 'Define a string variable StrEx = "Luke Skywalker" MsgBox Right(StrEx, Len(StrEx) - InStr(StrEx, " ")) 'Result is: "Skywalker" StrEx = "Leonardo da Vinci" MsgBox Right(StrEx, Len(StrEx) - InStr(StrEx, " ")) 'Result is: "da Vinci" StrEx = "May the Force be with you" MsgBox Right(StrEx, Len(StrEx) - InStr(StrEx, " ")) 'Result is: "the Force be with you " End SubVBA Right to Extract Last Word
To extract the last word from a string with a phrase, use the Right Function along with Len and InstrRev Function.
As we have seen above, VBA LEN function counts the number of characters in a string:
Len(StrEx)The VBA InStrRev Function searches for a substring inside a string and returns the position number of the substring. It starts the search from the end of the phrase (right to left) but returns the position from the start of the string (left to right).
InStrRev(StrEx, " ")By combining the functions, we can extract the part after the last space from a phrase:
Sub RightExample_6() Dim StrEx As String 'Define a string variable StrEx = "Luke Skywalker" MsgBox Right(StrEx, Len(StrEx) - InStrRev(StrEx, " ")) 'Result is: "Skywalker" StrEx = "Leonardo da Vinci" MsgBox Right(StrEx, Len(StrEx) - InStrRev(StrEx, " ")) 'Result is: "da Vinci" StrEx = "May the Force be with you" MsgBox Right(StrEx, Len(StrEx) - InStrRev(StrEx, " ")) 'Result is: "you" End SubTag » Code Vba Droite
-
Fonction VBA : Right - Excel
-
Right Function (Visual Basic For Applications) - Microsoft Docs
-
Fonction Droite En VBA | Excel-Downloads
-
VBA Right$() : Macro Pour Extraire Des Caractères à Droite
-
MS Excel: How To Use The RIGHT Function (WS, VBA) - TechOnTheNet
-
Excel - Fonction DROITE En VBA
-
Excel VBA Mid Left And Right: A Complete Guide
-
Excel VBA Fonction Len, Right, Left - YouTube
-
VBA RIGHT Function (Syntax + Example) - Excel Champs
-
Right Function In VBA Excel - WallStreetMojo
-
How To Use VBA Right Function In Excel? - EduCBA
-
How To Use VBA Right Function In Excel (6 Examples)
-
Manipuler Les Chaînes De Caractères, Extraire Texte Par VBA - Dophis
-
Manipuler Les Chaines De Caractères En VB6 Et VBA Excel - SilkyRoad