VBA LEN Function

VBA LEN Function

Overview of VBA LEN Function:

VBA LEN function 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. It could be used as both Worksheet and VBA function. The LEN function can be used 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.
Table of Contents:

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 use Macro Codes

Here are the step by step instructions to use the VBA LEN function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

Complete List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.
List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *