VBA Len Function

The VBA Len function returns the number of characters in a supplied string or the number of bytes required to store a supplied variable.

The syntax of the VBA Len function is:

Len( Expression )

Where the Expression argument is the string or variable that you require the length of.


VBA Len Function Examples

Example 1

' Example 1. Find the length of the string "John Michael Smith".
Dim strLen As Integer
strLen = Len( "John Michael Smith" )
' The variable strLen is now equal to 18.

In the example above, the VBA Len function returns the result 18.


Example 2

' Example 2. Return the length of an empty string "".
Dim strLen As Integer
strLen = Len( "" )
' The variable strLen is now equal to 0.

In the example above, the VBA Len function returns the value 0.


Further details and examples of the VBA Len function are provided on the Microsoft Developer Network.