How to use the LEFT function in VBA?

The Left function returns a specified number of characters from the left side of the given input string.


Left(String, Length)

Parameter Description

String − A required parameter. Input String from which the specified number of characters to be returned from the left side.
Length − A required parameter. An Integer, which specifies the number of characters to be returned.


Add a button and add the following function.

Private Sub Constant_demo_Click()
   Dim var as Variant
   var = "Microsoft VBScript"
   msgbox("Line 1 : " & Left(var,2))
   var = "MS VBSCRIPT"
   msgbox("Line 2 : " & Left(var,5))
   var = "microsoft"
   msgbox("Line 3 : " & Left(var,9))
End Sub

When you execute the above function, it produces the following output.

Line 1 : Mi
Line 2 : MS VB
Line 3 : microsoft