Excel VBA Left and Right functions

The Left and Right functions are used to chop characters from a string. Use Left to chop characters from the start of the string; use Right to chop characters starting from the end of the string. In between the round brackets of Left and Right you type the number of characters you want to chop. If you miss out the number of characters to chop then Left and Right extract just one character from the start or end of the string. Some example might clear things up.

 

Create a new Sub and try this code out (you can use your spreadsheet from the previous section for this:

Dim Email As String

Email = "myaddress@myisp.com"

MsgBox Left(Email, 9)

MsgBox Right(Email, 9)

The first two lines just set up a String variable and place an email address in the Email variable. The third line is a message box that uses the Left function:

MsgBox Left(Email, 9)

When you run the code you'll see that the message box displays the first 9 characters of the email address, everything to the left of the @ sign.

The fourth line is this:

MsgBox Right(Email, 9)

The Right function will display 9 characters starting from the final character in the email address, everything to the right of the @ sign.

That's fairly straightforward, we're sure you'll agree. But now for a more complex use of Left and Right.

Suppose you have a full name in cell A1 in this format:

David Gilmour

However, suppose you want to have the surname first then the first name. This format:

Gilmour, David

You can use Left, Right and the InStr Functions to achieve this.

Create a new Sub and call it LastFirst. Now set up four variables, three Strings and an Integer:

Dim FullName As String
Dim FirstName As String
Dim LastName As String
Dim SpacePos As Integer

Place the full name in the FullName variable:

FullName = "David Gilmour"

Now use InStr to locate the position of the space in the name:

SpacePos = InStr(FullName, " ")

To get just the first name you can start at the beginning of the full name and go up to the SpacePos minus 1:

FirstName = Left(FullName, SpacePos - 1)

The reason why you need to deduct 1 from the SpacePos variable is because the InStr function will return the position of the space, a value of 6 for our name. The final character of the first name, however, is 1 less than this, as David only has 5 characters in it.

To get the last name, we need something slightly different. The starting position is the length of the full name minus the length of the first name. This will get us the correct number of characters to grab starting from the right of the name. The code is this:

LastName = Right(FullName, Len(FullName) - Len(FirstName))

So as the final parameter of Right we have this:

Len(FullName) - Len(FirstName)

This uses the Len function to get the length of the FullName and FirstName variables.

Finally, display the results in a message box:

MsgBox (LastName & ", " & FirstName)

We have the LastName variable first and then the FirstName. The two are separated by concatenation symbols (&). We also need a comma, and we have this in double quotes so that VBA sees it as text. So we're saying, "Join together the Last Name, then a comma, then the First Name".

The whole of your code, then, should look like this:

Excel VBA code to reverse a first name and last name

Run your code and you should see this message box:

Excel Message box showing a reversed name

Click OK to return to your code. Now type a new name. Change this line, for example:

FullName = "David Gilmour"

to this:

FullName = "William Shakespeare"

Run your code again and the message box will display this:

An Excel message box with a reversed name

That final exercise illustrates that the more string functions you know and are comfortable with the more you can achieve in your programming.

(NOTE: The above code only works for names that have two parts. It will fall down if the name is, say, David Lloyd George. But there is an easier way to do the exercise above, and one that will cover names of any length: by using the Split function. You'll see how to achieve all this after we cover something called Arrays. If you want to jump ahead, the page is here: Arrays and the Split function.)

 

In the next lesson, we'll look at one final string function - Mid.