Excel VBA type Conversion Function

Source: Internet
Author: User

Cbool Functions

Description
Return expression. This expression has been converted to a variable of the Boolean (Boolean Type/type or not) subtype ).
Syntax
Cbool (expression)
Expression is any valid expression.

Description
If expression is zero, false is returned; otherwise, true is returned. If expression cannot be interpreted as a value, a runtime error occurs. The following example uses the cbool function to convert expression (expression) to boolean. If expression is not zero, cbool returns true; otherwise, false.
Dim a, B, check
A = 5: B = 5' initialization variable
Check = cbool (A = B) 'check value is true

A = 0' defines the variable
Check = cbool (a) 'check box is set to false

Cbyte Functions

Description
Returns an expression that has been converted to a byte (byte type) sub-type variant.
Syntax
Cbyte (expression)
The expression parameter is any valid expression.

Description
Generally, code can be written using a subtype Conversion Function to show that the results of some operations should be represented as specific data types, rather than the default type. For example, in the case of currency, single-precision, double-precision, or integer operations, use cbyte to force the byte operation.
The cbyte function is used to convert data from other data types to byte subtypes in internationally recognized formats. For example, the identification of a decimal separator (for example, a kilobytes) may depend on the system's regional settings.

An error occurs if expression is out of the acceptable range of byte subtypes. The following example uses the cbyte function to convert expression to byte:

Dim mydouble, mybyte
Mydouble = 125.5678 'mydouble is a double-precision value.
Mybyte = cbyte (mydouble) 'mybyte contains 126

Ccur Function

Description
Returns an expression that has been converted to a variant of the currency subtype.
Syntax
Ccur (expression)
The expression parameter is any valid expression.

Description
Generally, you can use the subtype Data Conversion Function to write code to show that the results of some operations should be expressed as specific data types rather than the default data types. For example, in the case of integer calculation, use the ccur function to force the currency calculation.
The ccur function is used to convert data from other data types to currency subtypes in internationally recognized formats. For example, the identification of the decimal separator and the thousands separator depends on the system's region settings.

The following example uses the ccur function to convert an expression to currency:
Dim mydouble, mycurr
Mydouble = 543.214588 'mydouble is double precision
Mycurr = ccur (mydouble * 2) 'converts the result of mydouble * 2 (1086.429176)
'Currency (1086.4292)

Cdate Function

Description
Returns the expression, which has been converted to the variant of the date subtype.
Syntax
Cdate (date)
The date parameter is any valid date expression.

Description
The isdate function is used to determine whether a date can be converted to a date or a time. Cdate identifies date text and time text, and some numbers within the acceptable date range. When a number is converted to a date, the integer part of the number is converted to a date, and the score part is converted to the time calculated from midnight.
Cdate specifies the recognition date format based on the system region. If the data format cannot be identified by the date setting, the correct order of year, month, and day cannot be determined. In addition, if the long date format contains a string representing the day of the week, it cannot be recognized.

The following example uses the cdate function to convert a string to a date. Generally, hard-coded dates and times are not recommended (as shown in the following example ). The text format of the date and time is used (for example, #10/19/1962 #, #4:45:23 #).

Mydate = "October 19,196 2" 'defines a date
My1_date = cdate (mydate) 'To date data type
Mytime = "4:35:47" 'defines the time
My1_time = cdate (mytime) 'To date data type

Cdbl Functions

Description
Returns an expression that has been converted to a variant of the double subtype.
Syntax
Cdbl (expression)
The expression parameter is any valid expression.

Description
Generally, you can use the subtype Data Conversion Function to write code to show that the results of some operations should be expressed as specific data types rather than the default data types. For example, in the case of currency or integer operations, use the cdbl or csng function to force double or single-precision arithmetic operations.
The cdbl function is used to convert data from other data types to double subtypes in internationally recognized formats. For example, the identification of the decimal separator and the thousands separator depends on the system's regional settings. The following example uses the cdbl function to convert expression to double.

Dim mycurr, mydouble
Mycurr = ccur (234.456784) 'mycurr is of the currency type.
Mydouble = cdbl (mycurr * 8.2*0.01) 'converts the result to the double type.

CINT Function

Description
Returns an expression that has been converted to a variant of the integer subtype.
Syntax
CINT (expression)
The expression parameter is any valid expression.

Description
Generally, code can be written using a subtype Conversion Function to show that the results of some operations should be represented as specific data types, rather than the default type. For example, in the case of currency, single-precision, or double-precision operations, use CINT or clng to force integer operations.
The CINT function is used to convert data from other data types to integer subtypes in internationally recognized formats. For example, the identification of a decimal separator (for example, a kilobytes) may depend on the system's regional settings.

An error occurs if expression is out of the acceptable range of integer subtypes.

The following example uses the CINT function to convert a value to an integer:

Dim mydouble, Myint
Mydouble = 2345.5678 'mydouble is double.
Myint = CINT (mydouble) 'myint contains 2346.

--------------------------------------------------------------------------------
Note that the CINT and fix functions are different from the int functions. The latter two take the fractional part of the number to the end of the integer, instead of rounding it. When the fraction is exactly 0.5, The CINT function usually rounds it to the nearest even number. For example, 0.5 is rounded to 0, and 1.5 is rounded to 2.
--------------------------------------------------------------------------------

Clng Function

Description
Returns an expression that has been converted to a variant of the long subtype.
Syntax
Clng (expression)
The expression parameter is any valid expression.

Description
Generally, you can use the subtype Data Conversion Function to write code to show that the results of some operations should be expressed as specific data types rather than the default data types. For example, in case of currency operations, single-precision or double-precision arithmetic operations, use the CINT or clng function to force integer operations.
The clng function is used to convert data from other data types to long subtypes in internationally recognized formats. For example, the identification of the decimal separator and the thousands separator depends on the system's region settings.

If the expression value is not within the range allowed by the long subtype, an error occurs.

The following example uses the clng function to convert the value to long:

Dim myval1, myval2, mylong1, mylong2
Myval1 = 25427.45: myval2 = 25427.55 'myval1, myval2 is a double-precision value.
Mylong1 = clng (myval1) 'mylong1 contains 25427.
Mylong2 = clng (myval2) 'mylong2 contains 25428.

--------------------------------------------------------------------------------

Note that the clng function has different functions from the fix and INT functions. The latter two functions take the rounded end of the decimal part of the value instead of rounding it. When the decimal part is equal to 0.5, The clng function is usually rounded to the nearest even number. For example, 0.5 is rounded to 0 and 1.5 is rounded to 2.

--------------------------------------------------------------------------------

Csng Function
Description
Returns an expression that has been converted to a single-child variant.
Syntax
Csng (expression)
The expression parameter is any valid expression.

Description
Generally, code can be written using a subtype Conversion Function to show that the results of some operations should be represented as specific data types, rather than the default type. For example, in the case of currency or integer operations, use cdbl or csng to force dual-precision or single-precision operations.
The csng function is used to convert data from other data types to single subtypes in internationally recognized formats. For example, the identification of a decimal separator (for example, a kilobytes) depends on the system's region settings.

An error occurs if expression is out of the range allowed by the single subtype.

The following example uses the csng function to convert the value to single:

Dim mydouble1, mydouble2, mysingle1, mysingle2' mydouble1, and mydouble2 are double-precision values.
Mydouble1 = 75.3421115: mydouble2 = 75.3421555
Mysingle1 = csng (mydouble1) 'mysingle1 includes 75.34211.
Mysingle2 = csng (mydouble2) 'mysingle2 includes 75.34216.

CSTR Function

Description
Returns an expression that has been converted to a string-type variant.
Syntax
CSTR (expression)
The expression parameter is any valid expression.

Description
Generally, code can be written using a subtype Conversion Function to show that the results of some operations should be represented as specific data types, rather than the default type. For example, use CSTR to forcibly represent the result as a string.
The CSTR function is used to replace the STR function for internationally recognized format conversion from other data types to string subtypes. For example, the identification of the decimal separator depends on the system's region settings.

The expression determines the returned data according to the following table:

If expression is CSTR
Boolean string, including true or false.
Date string, which contains the system's short date format date.
Null runtime error.
Empty zero-length string ("").
Error string, which contains the word "error" that follows the wrong number.
Other numeric strings, including this number.

The following example uses the CSTR function to convert a number to a string:

Dim mydouble, mystring
Mydouble = 437.324 'mydouble is a double value.
Mystring = CSTR (mydouble) 'mystring contains "437.324"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.