Excel VBA Left Function
VBA Left Function: The Left function in VBA returns a substring from the start of the specified string.
Syntax
Left (Str, Length)
Parameter
Str (required) – This parameter represents the string that you want to extract a substring.
Length (required) – This parameter represents the length of the substring.
Return
This function returns a substring from the start of a specified string.
Example 1
Sub LeftFunction_Example1() 'return a substring of length 4 from the start of the string. Dim str As String str = Left("Welcome to VBA world!", 3) ' The variable will return the text string "Wel". ActiveCell.Value = str End Sub
Output
Wel
Example 2
Sub LeftFunction_Example2() 'it will extract a substring of length 12 from the start of the string Dim str As String str = Left("Welcome to VBA World!", 12) 'The variable will return the text string "Welcome to V". ActiveCell.Value = str End Sub
Output
Welcome to V
Example 3
Sub LeftFunction_Example3() 'it will extract the first part of the string up to the first space. Dim pos As Integer Dim str As String pos = InStr(1, "Welcome to VBA world", " ") str = Left("JWelcome to VBA world", pos - 1) ' Now, the variables res will return "JWelcome". ActiveCell.Value = str End Sub
Output
JWelcom