VBA Function: Len

The VBA Len function returns the number of characters in a string.

Usage:

Len(text)


Examples of Usage

Using the Len function to get the number of characters in multiple strings:

Sub example()

    MsgBox Len("") 'Returns: 0
    MsgBox Len("45") 'Returns: 2
    MsgBox Len("excel") 'Returns: 5
    MsgBox Len("www.excel-pratique.com") 'Returns: 22
    
    myVariable = "example"
    MsgBox Len(myVariable) 'Returns: 7
    
End Sub

Using the Len function in a condition that checks if the username has at least 3 characters before performing an action:

Sub example()

    usrName = InputBox("Enter a username:", "Username")
    
    If Len(usrName) >= 3 Then
        MsgBox "Congratulations, " & usrName & "!"
    End If
    
End Sub