The VBA Left function returns a substring from the start of a supplied string.
The syntax of the function is:
Where the function arguments are:
Str | - | The original string that you want to extract a substring from. |
Length | - | The length of the substring. |
' Example 1. Extract a substring of length 4 from the start of the string "John Michael Smith".
Dim res As String
' The variable res is now equal to the text string "John".
res = Left( "John Michael Smith", 4 ) |
In the example above, the VBA Left function returns the result "John".
' Example 2. Extract a substring of length 12 from the start of the string "John Michael Smith".
Dim res As String
' The variable res is now equal to the text string "John Michael".
res = Left( "John Michael Smith", 12 ) |
In the example above, the VBA Left function returns the substring "John Michael".
' 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", " " )
' Now, the variables pos = 5 and res = "John".
res = Left( "John Michael Smith", pos - 1 ) |
In the example above:
Further information and examples of the VBA Left function are provided on the Microsoft Developer Network.