VBA LEN Function

VBA LEN Function in Excel

VBA LEN function in Excel is categorized as a Text/String function in VBA. It is a built-in function in MS Office Excel. It returns the length of a given string. It has one mandatory String parameter. If input string has Null, then it returns null. We use in both Worksheet and VBA function.
The LEN function can use in either procedure or function in a VBA editor window in Excel. We can use this VBA LEN function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the LEN function, where we can use this LEN function and real-time examples in VBA.

Syntax of VBA LEN Function

The syntax of the VBA LEN function is

Len(Expression)

Note: This LEN function founds the length of specified string.

Parameters or Arguments

This function has one mandatory parameter or argument for the LEN Function.
Where
Expression: The expression is a mandatory argument. The expression is the string which we want to know the length.

Where we can apply or use the VBA LEN Function?

We can use this VBA LEN function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Find Length of a string:”987654321″

Here is a simple example of the VBA LEN function. This below example macro returns the length a given string (Includes numbers).

'Find Length of a string:"987654321"
Sub VBA_Len_Function_Ex1()

    Dim sString As String, iCnt As Integer
    
    sString = "987654321"
    
    iCnt = Len(sString)
    
    MsgBox "The length of a given string is : " & iCnt, vbInformation, "VBA Len Function"

End Sub

Output: Here is the screen shot of the first example output.
VBA Len Function

Example 2: Find Length of a string:”Life is Beautiful.”

Here is a simple example of the VBA LEN function. This below example macro returns the length a given string (Includes space and dot).

'Find Length of a string:"Life is Beautiful."
Sub VBA_Len_Function_Ex()

    Dim sString As String, iCnt As Integer
    
    sString = "Life is Beautiful."
    
    iCnt = Len(sString)
    
    MsgBox "The length of a given string is : " & iCnt, vbInformation, "VBA Len Function"

End Sub

Output: Here is the screen shot of the second example output.
VBA Len Function

Example 3: Find Length of empty string:””

Here is a simple example of the VBA LEN function. This below example macro returns zero if it is empty string.

'Find Length of empty string:""
Sub VBA_Len_Function_Ex3()

    Dim sString As String, iCnt As Integer
    
    sString = ""
    
    iCnt = Len(sString)
    
    MsgBox "The length of a given string is : " & iCnt, vbInformation, "VBA Len Function"
End Sub

Output: Here is the screen shot of the third example output.
VBA Len Function

Example 4: Find Length of a string::”123$#@”

Here is a simple example of the VBA LEN function. This below example macro returns the length a given string (Includes special characters).

'Find Length of a string::"123$#@"
Sub VBA_Len_Function_Ex4()

    Dim sString As String, iCnt As Integer
    
    sString = "123$#@"
    
    iCnt = Len(sString)
    
    MsgBox "The length of a given string is : " & iCnt, vbInformation, "VBA Len Function"

End Sub

Output: Here is the screen shot of the fourth example output.
VBA Len Function

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply