Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Type conversion functions
In this articleEach function coerces an expression to a specific data type. Syntax
The required expression argument is any string expression or numeric expression. Return typesThe function name determines the return type as shown in the following:
RemarksIf the expression passed to the function is outside the range of the data type being converted to, an error occurs. Note Conversion functions must be used to explicitly assign LongLong (including LongPtr on 64-bit platforms) to smaller integral types. Implicit conversions of LongLong to smaller integrals are not allowed. In general, you can document your code using the data-type conversion functions to show that the result of some operation should be expressed as a particular data type rather than the default data type. For example, use CCur to force currency arithmetic in cases where single-precision, double-precision, or integer arithmetic normally would occur. You should use the data-type conversion functions instead of Val to provide internationally aware conversions from one data type to another. For example, when you use CCur, different decimal separators, different thousand separators, and various currency options are properly recognized depending on the locale setting of your computer. When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same type as is passed in. Use the IsDate function to determine if date can be converted to a date or time. CDate recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight. CDate recognizes date formats according to the locale setting of your system. The correct order of day, month, and year may not be determined if it is provided in a format other than one of the recognized date settings. In addition, a long date format is not recognized if it also contains the day-of-the-week string. A CVDate function is also provided for compatibility with previous versions of Visual Basic. The syntax of the CVDate function is identical to the CDate function; however, CVDate returns a Variant whose subtype is Date instead of an actual Date type. Since there is now an intrinsic Date type, there is no further need for CVDate. The same effect can be achieved by converting an expression to a Date, and then assigning it to a Variant. This technique is consistent with the conversion of all other intrinsic types to their equivalent Variant subtypes. Note The CDec function does not return a discrete data type; instead, it always returns a Variant whose value has been converted to a Decimal subtype. CBool function exampleThis example uses the CBool function to convert an expression to a Boolean. If the expression evaluates to a nonzero value, CBool returns True, otherwise, it returns False.
CByte function exampleThis example uses the CByte function to convert an expression to a Byte.
CCur function exampleThis example uses the CCur function to convert an expression to a Currency.
CDate function exampleThis example uses the CDate function to convert a string to a
Date. In general, hard-coding dates and times as strings (as shown in this example) is not recommended. Use date literals and time literals, such as
CDbl function exampleThis example uses the CDbl function to convert an expression to a Double.
CDec function exampleThis example uses the CDec function to convert a numeric value to a Decimal.
CInt function exampleThis example uses the CInt function to convert a value to an Integer.
CLng function exampleThis example uses the CLng function to convert a value to a Long.
CSng function exampleThis example uses the CSng function to convert a value to a Single.
CStr function exampleThis example uses the CStr function to convert a numeric value to a String.
CVar function exampleThis example uses the CVar function to convert an expression to a Variant.
See also
Support and feedbackHave questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback. |