VBA Left Function

Related Functions:
VBA Right
VBA Mid

Description

The VBA Left function returns a substring from the start of a supplied string.

The syntax of the function is:

Left( Str, Length )

Where the function arguments are:

Str - The original string that you want to extract a substring from.
Length - The length of the substring.


VBA Left Function Examples

Example 1

' Example 1. Extract a substring of length 4 from the start of the string "John Michael Smith".
Dim res As String
res = Left( "John Michael Smith", 4 )
' The variable res is now equal to the text string "John".

In the example above, the VBA Left function returns the result "John".


Example 2

' Example 2. Extract a substring of length 12 from the start of the string "John Michael Smith".
Dim res As String
res = Left( "John Michael Smith", 12 )
' The variable res is now equal to the text string "John Michael".

In the example above, the VBA Left function returns the substring "John Michael".


Example 3

' Example 3. Extract the first part of the string "John Michael Smith", up to the first space.
Dim pos As Integer
Dim res As String
pos = InStr( 1, "John Michael Smith", " " )
res = Left( "John Michael Smith", pos - 1 )
' Now, the variables pos = 5 and res = "John".

In the example above:


Further information and examples of the VBA Left function are provided on the Microsoft Developer Network.