VBA LEFT Function

The VBA LEFT Function helps to get the number of characters or substring only from the left side of the String or from the user-provided value. When this function is used in VBA code, it returns a part of the String from the beginning. Specifically, the VBA LEFT function only returns the substring that begins at the leftmost position of the input string.

This function is helpful in Excel both as a VBA function and as a worksheet function. It can be included in the formula in the cell. The VBA LEFT function is in the group of functions called “text.” It works the same way as the LEFT function in an Excel worksheet.

The VBA Left function takes a string and gives back a string-based result. If the string argument given to this function is Null, it will return Null. As its name suggests, the VBA Left function only takes the characters on the left side of the String. When the LEFT and the InStr function are used, we can find the space in VBA. It makes it easy for the user to tell which word is in the given String.

Syntax

Left (String, Length)

Parameter

String (required): The String argument denotes the length of the String we want to extract.

Length (required): It is the number of characters from the left portion of the specified String you want to get.

Example:

Here are the steps for writing a VBA macro to use the VBA Left function to get the first name from the given String:

Step 1: Open the tab for the VBA developer. Open the Excel worksheet, click on the ribbon tab, the developer window, the visual basic editor, or just press Alt+F11.

Step 2: You will see the VB Editor window. Next, you need to do a module. Click on Insert on the ribbon, then click on Module.

VBA Module

Step 3: You’ll see that VBA adds a new window for the Module. The programme’s first step is to declare the variables and then to name your macro.

Sub VBA_LEFT_Function()

Dim str_Left_String As String

End Sub

Step 4: Now add the LEFT function. We will put the String in the arguments and 5 in the length argument because we want to get the first five characters from the left side of the String. Put the value of fetch into our variable, and then use MsgBox to return the variable.

Sub VBA_LEFT_Function()

Dim str_Left_String As String

str_Left_String = "Hello World"

MsgBox Left(str_Left_String, 5)

End Sub

Output

Either click Run or press the F5 key to run your macro. It will cause VBA to launch a MsgBox showing the first five characters of your String, starting from the left.

VBA Left Function Output

Leave a Comment

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

Scroll to Top