Return to VBA Code Examples

VBA CInt Function – Convert to Integer

This tutorial will demonstrate how to use the CInt VBA function to convert an expression to the integer data type.

CInt Function

VBA CInt Convert Expression to Integer

The VBA CInt function can be used to convert expressions to integer data type inside VBA code. The resulting number is rounded to become an integer.

VBA CInt Rounding

The VBA CInt function will round the decimal part of a number type or a number like expression. However, it does not round correctly in all cases. When the decimal part is 0.5 then VBA CInt function returns the closest even integer.

We can add a decimal number relatively small to our expected decimal value to change the behavior of VBA Cint function to the expected.

VBA CInt Converting Strings to Integers

The VBA CInt function can be used to convert strings to integers if the characters in the string have a meaning as numbers.

VBA CInt Run-Time Error 13 Type Mismatch

Using VBA Cint function with strings that contain non-numerical characters or characters that don’t have meaning in numerical context will result in a Run-Time error ’13’: Type mismatch.

VBA CInt Run-Time Error 6 Overflow

Using VBA Cint function with strings that result in a value smaller or bigger than the expected integer will result in a Run-Time error ’6’: Overflow. Integer data type in excel has an expected value of -32768 to 32767.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

VBA CInt Regional Settings

VBA CInt function has different behavior converting strings with comma or dot.  It uses the Regional Settings of the operating system for decimal separator and digit separator.

VBA CInt Converting Booleans to Integers

VBA Cint function can convert boolean variables to integers. If the evaluated expression is true the resulting integer is -1 and if the evaluated expression is false, the resulting integer is 0.

VBA CInt Converting Dates to Integers

VBA Cint function can convert a date variable to an integer. The returned value is the internal number used by excel for date storage rounded. If that number is outside of the expected integer limits for VBA then we get a  Run-Time error ’6’: Overflow.