Share via


Calculating String Length

Often you must know the length of a string to parse its contents. You can use the Len function to calculate the length of a string:

Dim lngLen As Long
lngLen = Len(strText)

When Microsoft® Visual Basic® for Applications (VBA) stores a string in memory, it always stores the length of the string in a long integer at the beginning of the string. The Len function retrieves this value and is therefore quite fast.

The Len function is useful when you must determine whether a string is a zero-length string (""). Rather than comparing the string in question to a zero-length string to determine whether they're equivalent, you can simply check whether the length of the string is equal to 0. For example:

If Len(strText) > 0 Then
   ' Perform some operation here.
End If

See Also

Getting the Most Out of Visual Basic for Applications | Working with Strings | Comparing Strings | Searching a String | Returning Portions of a String | Working with Strings as Arrays | Replacing Text Within a String | Converting Strings | Working with String Variables