The VBA CDec function converts an expression into a Decimal data type.
The syntax of the function is:
Where the Expression argument is the expression that that you want to convert to a Decimal.
' Convert strings and numeric values into Decimals
Dim dec1 As Variant
Dim dec2 As Variant
dec1 = CDec( "1,001.0000001" )
dec2 = CDec( 5 / 3 )' dec1 is now equal to the Decimal 1001.0000001 ' dec2 is now equal to the Decimal 1.66666666666667 |
After running the above VBA code, the variable dec1 = 1001.0000001 and the variable dec2 = 1.66666666666667.
If the CDec function is supplied with a text string that cannot be converted into a numeric value, it will return the error: