What is VBA CDEC Function
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.
how to use it
To use VBA's CDEC function you need to understand its syntax and arguments:
- Expression: The value which you want to convert into the decimal data type.
Example to use CDEC Function in VBA
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.
Below are some important points which you need to take care while using CDEC function in VBA.
- 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.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.