VBA LEFT Function

VBA Left Function

VBA Left function is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns the specified number of characters from a given string, starting from the left most character. It has two mandatory parameters. It can be used as a Worksheet function or VBA function. The VBA Left function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA Left function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Left function, where we can use this VBA Left function and real-time examples.

Table of Contents

Syntax of VBA Left Function

The syntax of the VBA Left function is

Left (string, [length])

Note: This Left function returns a substring (Specified number of characters) from left side.

Parameters or Arguments

There are two mandatory parameters or arguments for the Left Function.
Where
string: The string is a mandatory argument. The string from which we want to extract a substring.
Length: The length is a required argument. It represents the number of characters of the substring that you want to extract from a string.

Where we can apply or use the VBA Left Function?

We can use this VBA Left function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Extract a substring of length 5 from starting of the string

Here is a simple example of the VBA Left function. This below example macro uses the Left function and extracts the left most four characters from a given string.

'Extract a substring of length 5 from starting of the string.
Sub VBA_LEFT_Function_Ex1()

    Dim sString As String, sSubString As String
    
    sString = "Life is Beautiful"
    
    sSubString = Left(sString, 4)
    
    MsgBox "The substring is :" & sSubString, vbInformation, "VBA Left Function"

End Sub

Output:
Here is the screen shot of first example output.
VBA Left Function

Example 2: Extract a substring of length 30 from starting of the string

Here is another example of the VBA Left function. This below example macro uses the Left function and extracts the left most 30 characters from a given string. There are only 17 available characters. So, it displays only 17 characters instead of 30 characters.

'Extract a substring of length 30 from starting of the string.
Sub VBA_LEFT_Function_Ex2()

    Dim sString As String, sSubString As String
    
    sString = "Life is Beautiful"
    
    sSubString = Left(sString, 30)
    
    MsgBox "The substring is :" & sSubString, vbInformation, "VBA Left Function"

End Sub

Output:
Here is the screen shot of second example output.
VBA Left Function

Example 3: Get First Name from Full Name using Left Function

Here is one more example. We are extracting a first name from full name using Instr and Left function.

'Get First Name from Full Name using Left Function
Sub VBA_LEFT_Function_Ex3()

    Dim sString As String
    Dim sFName As String
    Dim sPosition As Integer
    Dim sSubString As String
    
    sString = "F_Name L_Name"
    sPosition = InStr(1, sString, " ")
    sFName = Left(sString, sPosition - 1)
    
    MsgBox "The First Name is :" & sFName, vbInformation, "VBA Left Function"

End Sub

Output:
Here is the screen shot of third example output.
VBA Left Function

Example 4: Get Email Id from email using Left Function

Here is another example for Left function. Here getting Email Id from email using Instr and Left functions.

'Get Email Id from email using Left Function
Sub VBA_Left_Function_Ex4()

    Dim sString As String
    Dim sEName As String
    Dim sPosition As Integer
    Dim sSubString As String
    
    sString = "ABCD@hotmail.com"
    sPosition = InStr(1, sString, "@")
    sEName = Left(sString, sPosition - 1)
    
    MsgBox "Specified Email Id is :" & sEName, vbInformation, "VBA Left Function"

End Sub

Output:
Here is the screen shot of fourth example output.
VBA Left Function

Instructions

Here are the step by step instructions to use the VBA LEFT function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

Quick View Examples & Results

You can find quick view of Left function examples and its results.

'VBA Left Function Quick View Examples
Sub VBA_Left_Function()

    Dim sString As String, sSubString As String
    sString = "FName LName"
    
    sSubString = Left(sString, 5)  'Result: 'FName'
    Debug.Print sSubString
    sSubString = Left(sString, 6)  'Result: 'FName '
    Debug.Print sSubString
    sSubString = Left(sString, 50)  'Result: 'FName LName'
    Debug.Print sSubString
    sSubString = Left(sString, 0)  'Result: Displays Nothing
    Debug.Print sSubString
    sSubString = Left(sString, -1) 'Result: Displays Error
    Debug.Print sSubString
    
End Sub

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA Left function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *