The VBA CInt function converts an expression into an Integer.
The syntax of the function is:
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.
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" )int2 = CInt( 3.95 )
' int1 is now equal to the value 10
' 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.
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:
If the CInt function is supplied with a text string that cannot be converted into a numeric value, it will return the error: