How to use the VBA LEN Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA LEN Function (Syntax + Example)

The VBA LEN function is listed under the text category of VBA functions. When you use it in a VBA code, it returns the number of characters in a string. In simple words, you can count the number of characters of a string. It works like the LEN function in the worksheet.

Syntax

Len(Expression)

Arguments

  • Expression: The expression for which you want to count the characters.

Example

To practically understand how to use VBA LEN function, you need to go through the below example where we have written a vba code by using it:

Sub example_LEN()
Range("B1").Value = Len(Range("A1"))    
End Sub

In the above code, we have used the LEN to count the number of characters from the cell A1 (Excel Champs) and in the result, it has returned 12 in the result as the value “Excel Champs” has 12 characters including the space between the words.

Notes

  • If the supplied string is NULL it will return NULL in the result.

Related Functions

FORMAT | INSTR | INSTRREV | LCASE | LEFT | LTRIM | MID | REPLACE | RIGHT | RTRIM | SPACE | STRCOMP | STRCONV | STRING | STRREVERSE | TRIM | UCASE