Len Function

Description

Returns the number of characters in a string or the number of bytes required to store a variable.

Syntax

Len(string | varname)

The Len function syntax has these parts:

Part

Description

string

Any valid string expression. If string contains Null, Null is returned.

varname

Any valid variable name. If varname contains Null, Null is returned. If varname is a Variant, Len treats it the same as a String and always returns the number of characters it contains.


Caution Len may not be able to determine the actual number of storage bytes required when used with user-defined data types.

Note Another function (LenB) is provided for use with byte data contained in a string. Instead of returning the number of characters in a string, LenB returns the number of bytes used to represent that string.

See Also

Data Type Summary, InStr Function.

Example

This example uses the Len function to return the number of characters in a string or the number of bytes required to store a variable. The Type...End Type block defining CustomerRecord must be preceded by the keyword Private if it appears in a Class module. In a standard module, a Type statement can be Public. To create a standard Module, choose Module from the Insert menu (your host application may differ, for example in Microsoft Excel, select Macro from the Insert menu and then choose Module).


Type CustomerRecord                    ' Define user-defined type.
    ID As Integer                    ' Place this definition in a 
    Name As String * 10                ' standard module.
    Address As String * 30Type
Customer As CustomerRecord        ' Declare variables.MyInt As Integer, MyCur As Currency= "Hello World"                ' Initialize variable.= Len(MyInt)                    ' Returns 2.= Len(Customer)                ' Returns 42.= Len(MyString)                ' Returns 11.= Len(MyCur)                    ' Returns 8.