VBA Functions: CInt and CLng
The VBA CInt and CLng functions convert a numeric value to an integer by rounding to the nearest integer.
Usage:
CInt(value)
CLng(value)
Example of Usage
Using the CInt function to convert different types of numeric values:
Sub example()
MsgBox CInt(1) 'Returns: 1
MsgBox CInt(-1) 'Returns: -1
MsgBox CInt(2.9) 'Returns: 3
MsgBox CInt(-2.9) 'Returns: -3
MsgBox CInt(4.5) 'Returns: 4
MsgBox CInt(-4.5) 'Returns: -4
MsgBox CInt(0.424) 'Returns: 0
MsgBox CInt("13") 'Returns: 13
MsgBox CInt("-4.5") 'Returns: -4
MsgBox CInt(True) 'Returns: -1
MsgBox CInt(False) 'Returns: 0
End Sub
If the number to be converted is too large for an Integer, convert it to a Long using the CLng function:
Sub example()
MsgBox CLng(123456.789) 'Returns: 123457
End Sub