Return to VBA Code Examples

VBA Right Function – Extract Text From Right

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:

VBA 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.

VBA 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.

right cell value vba

 

 

 

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:

By combining the functions, we can remove a certain number of characters from the start of the string:

VBA 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:

The VBA InStr function searches for a substring inside a string and returns the position number of the substring.

By combining the functions, we can extract the part after the first space from a phrase:

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

VBA 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:

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).

By combining the functions, we can extract the part after the last space from a phrase: