The VBA CDEC function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it converts a number into the decimal data type. In simple words, with decimal data type a number can have 28 decimals where the largest value is +/-7.9228162514264337593543950335, and the smallest nonzero value is +/-0.0000000000000000000000000001.
- Expression: The value which you want to convert into the decimal data type.
To practically understand how to use VBA CDEC function, you need to go through the below example where we have written a vba code by using it:
Sub example_CDEC() Range("B1").Value = CDec(Range("A1") / Range("A2")) End Sub
In the above example, we have divided the value from the cell A1 (10) with the value from cell A2 (9) and then we have used the CDEC function to convert that value into decimal data type and it has returned 1.11111111111111 in the result.
- If the value specified is a value other than a number or a number which can’t be recognized as a number, VBA will return the run-time 13 error.
- It will also return an error if the value supplied is out of the range of decimal data type.