Excel VBA Len Function

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

Syntax

Len (Expression)

Parameter

Expression (required)- This parameter represents the string or variable of which you want to evaluate the length of.

Return

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

Example 1

Sub StrLenFunction_Example1()
 'evaluating the length of the string.
 Dim str As String, strLen As Integer
 str = "Welcome to VBA World!"
 strLen = Len(str)
 ' The variable will return 21.
 ActiveCell.Value = strLen
 End Sub 

Output

21

Excel VBA Len Function

Example 2

Sub StrLenFunction_Example2()
 'evaluating the length of an empty string "".
 Dim strLen As Integer
 strLen = Len("")
 ' The variable will return 0.
 ActiveCell.Value = strLen
 End Sub 

Output

0

Excel VBA Len Function