How to use Left function in VBA?

The Left function in VBA (Visual Basic for Applications) is used to extract a specified number of characters from the left side of a string. Syntax:

Left(Text, Length)
Text is the string from which you want to extract characters. Length is the number of characters you want to extract from the left side of the string. Here’s an example of how to use the Left function in VBA:

Sub LeftFunctionExample()
    Dim originalString As String
    Dim resultString As String
    Dim numberOfCharacters As Integer
    ' Initialize your variables
    originalString = "Hello, World!"
    numberOfCharacters = 5

    ' Use the Left function to get the first 5 characters from originalString
    resultString = Left(originalString, numberOfCharacters)
    ' Output result to Immediate Window (press Ctrl+G to view the Immediate Window in the VBE)
    Debug.Print resultString  ' Output will be "Hello"
End Sub
In this example, the Left function will return the first 5 characters of the originalString, which are “Hello”. Remember that if the Length specified is greater than the actual length of the text, the entire text will be returned, and if it is zero or negative, an empty string (“”) will be returned. You can run the sub above by pasting it into a module in the Visual Basic Editor (VBE) within Excel and pressing F5 or by assigning it to a button or event within your Excel workbook.

Unlock Your Potential


Basic - Advanced


Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project

Leave a Reply

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