Left, Left\$ Functions

See Also              Example

Returns the leftmost n characters of a string argument.

Syntax

Left[\$](strexpr, n)

Remarks

Left returns a Variant; Left\$ returns a String.

The Left[\$] function has these parts:

Part               Description

strexpr           String expression from which the leftmost characters are returned.  This can be any string expression.  However, only Left can accept a Variant of VarType 1 (Null) as stringexpr, in which case, a Null is returned.

n                   Long integer expression indicating how many characters to return.  It must be between 0 and approximately 65,535, inclusive.  If n is 0, the return value is a zero-length string.  If n is greater than or equal to the number of characters in strexpr, the entire string is returned.

To find the number of characters in strexpr, use Len(strexpr).

See Also

Len Function

Mid, Mid\$ Function

Right, Right\$ Function

Left, Left\$ Function Example

The example uses the Left function to return the first of two words input by the user.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

Sub Form_Click ()

Dim LWord, Msg, RWord, SpcPos, UsrInp       ' Declare variables.

Msg = "Enter two words separated by a space."

UsrInp = InputBox(Msg)                      ' Get user input.

SpcPos = InStr(1, UsrInp, " ")              ' Find space.

If SpcPos Then

LWord = Left(UsrInp, SpcPos - 1)         ' Get left word.

RWord = Right(UsrInp, Len(UsrInp) - SpcPos)    ' Get right word.

Msg = "The first word you entered is """ & LWord

Msg = Msg & "." & """"  & " The second word is """

Msg = Msg & RWord & "." & """"

Else

Msg = "You didn't enter two words."

End If

MsgBox Msg                                  ' Display message.

End Sub