The VBA CLng Function

Description

The VBA CLng function converts an expression into a Long data type.

The syntax of the function is:

CLng( Expression )

Where the Expression argument is the expression that that you want to convert to a Long.

The Long data type can hold integer values between -2,147,483,648 and 2,147,483,647. Therefore, the supplied Expression must be able to be intepreted (or converted into) an integer between -2,147,483,648 and 2,147,483,647.


VBA CLng Function Examples

The following VBA code shows how the CLng function can be used to convert text and numeric values into Long data types.

' Convert Two Values Into Long Data Types
Dim lng1 As Long
Dim lng2 As Long
lng1 = CLng( "1,001" )
' lng1 is now equal to the value 1001
lng2 = CLng( 60000.9 )
' lng2 is now equal to the value 60001

After running the above VBA code, the variables lng1 and lng2 are equal to 1001 and 60001 respectively.

Note that the value 60000.9 was rounded to the nearest integer by the CLng function.


VBA CLng Function Errors

If the CLng function is supplied with a numeric value that is less than -2,147,483,648 or greater than 2,147,483,647, it will return the error:

Run-time error '6': Overflow

VBA Run Time Error 6 Message Box


If the CLng function is supplied with a text string that cannot be converted into a numeric value, it will return the error:

Run-time error '13': Type mismatch

VBA Run Time Error 13 Message Box