Name
Len, LenB Functions
Syntax
Len(string|varname)
-
string Use: Required
Data Subtype: String
A valid string expression.
-
varname Use: Required
Data Subtype: Any except object
A valid variable name.
Return Value
A Variant with subtype Long.
Description
Counts the number of characters within a string or the size of a
given variable. Use LenB to determine the actual
number of bytes required to hold a given variable in memory.
Rules at a Glance
stringandvarnameare mutually exclusive; that is, you must specify eitherstringorvarname, but not both.If either
stringorvarnamecontainsNull,LenreturnsNull.When you use
LenBwith byte data or a Unicode string,LenBreturns the number of bytes that represent the data or the string.You can’t use
Lenwith an object variable.If
varnameis an array, you must also specify a valid subscript. In other words,Lencan’t determine the total number of elements in or the total size of an array.
Programming Tips & Gotchas
Nonstring variants are treated the same as a string variables, and
Lenreturns the actual number of characters stored to the variable. But this can lead to unexpected results. Take the following snippet as an example:Dim vVar vVar = 100 MsgBox Len(vVar)You may expect the
Lenfunction to return 2 becauseiVaris obviously a variant of subtype integer. In fact,Lenreturns 3: the number of characters contained within the variant. In other words, theLenandLenBfunctions, when used with nonstring variants, return the number of characters ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access