Name

Len, LenB Functions

Syntax

Len(string | varname)
LenB(string | varname)
string

Use: Required

Data Type: String

A valid string literal or string expression.

varname

Use: Required

Data Type: Any except Object

A valid variable name.

Return Value

Long.

Description

Len returns the number of characters in a string or variable. LenB returns the number of bytes required to store a string in memory.

Rules at a Glance

  • string and varname are mutually exclusive; that is, you must specify either string or varname, but not both.

  • If either string or varname is Null, Len and LenB return Null.

  • You can’t use Len or LenB with an object variable.

  • If varname is an array, you must also specify a valid subscript. In other words, Len and LenB can’t determine the total number of elements in or the total size of an array. To determine the size of an array, use the LBound and UBound functions.

Programming Tips and Gotchas

  • Nonstring data is treated the same as strings when passed to the Len and LenB functions. For example, in the code:

    Dim number
    number = 100
    WScript.Echo Len(number)

    the Len function returns 3, since that is the number of characters in the value of number.

  • LenB is intended to work with string data, and returns the number of bytes required to store that string. If a nonstring data type is passed to the LenB function, its value is first converted to a string before its length is determined.

VBA/VBScript Differences

Although the Len and LenB functions handle strings identically in VBA and VBScript, they handle non-string ...

Get VBScript in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.