VBA CLng Function

VBA CLng Function

Objective of VBA CLng Function:


VBA CLng Function is categorized as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CLng function converts an expression to a Long data type.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CLng Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CLng function, where we can use this VBA CLng Function and it’s real-time examples.

Syntax of VBA CLng Function:

The syntax of the CLng Function in VBA is

CLng(Expression)

The CLng function returns a long data type value.

Parameters or Arguments:

The CLng function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a value. It is used to convert the value to a long value.

Where we can apply or use VBA CLng Function?

We can use this CLng Function in VBA 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: Convert a String(100) to Long Data Type

Here is a simple example of the VBA CLng function. This below specified expression(100) converts to a long type value.

'Convert a String(100) to Long Data Type
Sub VBA_CLng_Function_Ex1()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Long
    
    sValue = 100
        
    dResult = CLng(sValue)
        
    MsgBox "String(100) to Long Data Type : " & dResult, vbInformation, "VBA CLng Function"
    
End Sub

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

Example 2: Convert a Value(12345.678) to Long Data Type

Here is a simple example of the VBA CLng function. This below specified expression(12345.678) converts to a long type value.

'Convert a Value(12345.678) to Long Data Type
Sub VBA_CLng_Function_Ex2()

    'Variable declaration
    Dim sValue As Double
    Dim dResult As Long
    
    sValue = 12345.678
        
    dResult = CLng(sValue)
        
    MsgBox "Value(12345.678) to Long Data Type : " & dResult, vbInformation, "VBA CLng Function"
    
End Sub

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

Example 3: Convert a Value(4000000000) to Long Data Type (Returns an Overflow Error)

Here is a simple example of the VBA CLng function. This below specified expression(4000000000) converts to a long type value. It returns an Overflow error. The value 4,000,000,000 is greater than 2,147,483,647. The long value should be in between -2,147,483,648 to 2,147,483,647.

'Convert a Value(4000000000) to Long Data Type (Returns an Overflow Error)
Sub VBA_CLng_Function_Ex3()

    'Variable declaration
    Dim sValue
    Dim dResult As Long
    
    sValue = 4000000000#
        
    dResult = CLng(sValue)
        
    MsgBox "Value(4000000000) to Long Data Type : " & dResult, vbInformation, "VBA CLng Function"
End Sub

Output: Here is the screen shot of the third example output.
VBA Runtime Overflow Error

Example 4: Convert a string(Test) to Long Data Type (Returns an Type mismatch Error)

Here is a simple example of the VBA CLng function. This below example specified expression(Test) converts to a long type value. It returns Type mismatch error.

'Convert a string(Test) to Long Data Type (Returns an Type mismatch Error)
Sub VBA_CLng_Function_Ex4()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Long
    
    sValue = "Test"
        
    dResult = CLng(sValue)
        
    MsgBox "String(Test) to Long Data Type : " & dResult, vbInformation, "VBA CLng Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA Runtime Overflow Error

Instructions to use Macro Codes

Here are the step by step instructions to use the CLng 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’.

List of VBA Functions:

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

VBA Data Type Conversion Functions List of VBA Functions

Leave a Comment

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