The VBA CInt Function

Description

The VBA CInt function converts an expression into an Integer.

The syntax of the function is:

CInt( Expression )

Where the Expression argument is the expression that that you want to convert to an Integer.

The Integer data type can hold integer values between -32,768 and 32,767. Therefore, the supplied Expression must be able to be intepreted (or converted into) an integer between -32,768 and 32,767.


VBA CInt Function Examples

The following VBA code shows how the CInt function can be used to convert text and numeric values into Integers.

' Convert Two Values Into Integers
Dim int1 As Integer
Dim int2 As Integer
int1 = CInt( "10" )
' int1 is now equal to the value 10
int2 = CInt( 3.95 )
' int2 is now equal to the value 4

After running the above VBA code, the variables int1 and int2 are equal to 10 and 4 respectively.

Note that the value 3.95 was rounded to the nearest integer by the CInt function.


VBA CInt Function Errors

If the CInt function is supplied with a numeric value that is less than -32,768 or greater than 32,767, it will return the error:

Run-time error '6': Overflow

VBA Run Time Error 6 Message Box


If the CInt 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