Excel Snippets

VBA Left Function

The Excel VBA Left function extracts characters from the left side of an expression, beginning with the first character in the expression. A user-specified number sets the length of the subset returned, which is a string data type and includes any blank spaces.

Syntax:

Left(Expression, N)

The VBA Left function accepts two arguments, as described below.

Arguments:

Expression - the expression from which a substring on the left is extracted
N - number of characters from the left to return

Notes about usage:

  1. Expression may be any valid VBA data type, except array or object.
  2. Left will accept an element of an array for the expression.
  3. If Expression is a zero-length string (""), then a zero-length string ("") is returned for all valid values of N.
  4. If N is a fractional number, then it is rounded to the nearest Long integer.
  5. If the rounded value of N is a negative number, then an error message is generated and the program execution ends.
  6. If the rounded value of N is 0, then a zero-length string ("") is returned.
  7. If N is greater than or equal to the number of characters in Expression, the entire string is returned.
  8. The value returned is string, regardless of the expression data type.
  9. Using an expression of array or object data type results in an error message and program execution stops.
  10. The number of characters to return is a required argument. If omitted, an error messages is generated and program execution stops. Note: This function can also be used as part of an Excel spreadsheet formula, and its behavior here is different. Here, a zero-length string is returned ("").

How to use the VBA Left function

Dim str1 As String

str1 = "Last Name, First Name, Address"

MsgBox Left(str1, 1)

' Returns "L"

MsgBox Left(str1, 4)

' Returns "Last"

MsgBox Left(str1, 8)

' Returns "Last Nam"

In the first use of left, the first letter "L" is returned. Asking for four (4) characters to be returned results in "Name." With a request for eight (8) characters to be returned results in "Last Nam," since blank spaces count as characters.

Use the VBA Left function with extra spaces

Here, we consider two expressions conveying the same textual message, but one has extra blank spaces.

Dim str1, str2 As String

Str1 = "This example has lots of spaces"

str2 = " This example has lots of spaces "

MsgBox Left(str1, 1)

' Returns "T"

MsgBox Left(str2, 1)

' Returns " "

MsgBox Left(str1, 16)

' Returns "This example has"

MsgBox Left(str2, 16)

' Returns " This example "

The first string, str1, is of length thirty-one (31) characters, while str2 is of length forty-one (41). If we use Left to extract the first letter of either string, the value returned with str1 is the desired "T," while we will get a space with str2. And the difference between the letters extracted is even more pronounced when asking for sixteen (16).

Imagine having a list of names from which you need the initial or the first name, for example. Having extra unknown spaces can lead to incorrect results.

Using the VBA Left function without a length argument

Dim str1 As String

Str1 = "This example has lots of spaces"

MsgBox Left(str1)

If the length argument is omitted from the Left function, then VBA generates a Compile error saying that the argument is not optional. Further program processing ends.

Compile error with missing required argument
Error message when omitting the length argument from the VBA Left function

Interact with an Excel spreadsheet using a User-defined Left function

Create a VBA user-defined function and name it leftEX in which the return value from the VBA built-in Left function is the return value of leftEX.

create a user-defined function to act like the VBA built-in Left function
Create a user-defined left function in VBA to interact with a spreadsheet

Next, test that the function leftEX returns the correct value.

Sub testleftEX()

Dim str1, str2 As String

Dim N As Long

str1 = "LastName, FirstName"

N = 8

MsgBox Left(str1, 8)

' Returns "LastName"

str2 = leftEX(str1, N)

MsgBox str2

' Returns "LastName"

End Sub

Both return the same value.
Test the user-defined left function

Using the built-in function Left directly in a VBA subroutine returns the same value as calling the user-defined function leftEX. Next enter "LastName FirstName" in A1 (Cells(1,1)) of a spreadsheet. Then select B1.

Enter data into cell A1 in a spreadsheet
Enter data into an Excel spreadsheet

Just above B1, click on the function insert button fx. An insert function dialog box appears.

open the Insert Function dialog box
How to use a VBA function in an Excel spreadsheet via the Insert Function dialog box

Use the select a category drop-down menu and set to User Defined. Then in Select a Function, scroll down to leftEX, the function we created. Click the OK button.

A Function Arguments dialog box appears. Excel creates input boxes in this dialog box based on the arguments defined in the function selected.

Open the Function Arguments dialog box
VBA Functions Arguments dialog box creates argument input boxes from user syntax of a function.

Enter the cell A1, where the data is stored. Then input eight (8) in the input box next to N. This will tell Excel to select the first eight (8) left-most characters in the data in A1.

The VBA user-deined left function returns the same value as the built-in Left function.
A User-defined VBA function can behave like a VBA built-in function when interacting with an Excel spreadsheet.

Because we selected B1 before this point, leftEX will write the result in B1 when the OK button is clicked.

Note: Be careful that you did not select a cell with data in it, as that data will be overwritten, unless, of course, you want to overwrite the data.

Interact with an Excel spreadsheet using the User-defined Left function while omitting the length argument

Repeat the same steps as in Example 4, but do not enter a value for N.

Omit the number of characters to extract from a string when using the Function Arguments dialog box
Use the Function Argument dialog box, but omit inputting one of the arguments.

Click OK.

Error messages in Excel formulas differ from those in VBA functions
The Excel formula error message differs from the VBA function error message

Instead of "LastName" or any other characters, an error flag #VALUE! is recorded in B1.

Note: This example was constructed to show that error messages in Excel functions differ from those in VBA. Excel error messages are not as informative as those generated within a VBA subroutine.

See Also:

Excel VBA Built-in Functions

Interact with an Excel Spreadsheet

Recommended: