The VBA CDec Function


The VBA CDec function converts an expression into a Decimal data type.

The syntax of the function is:

CDec( Expression )

Where the Expression argument is the expression that that you want to convert to a Decimal.

VBA CDec Function Example

' Convert strings and numeric values into Decimals
Dim dec1 As Variant
Dim dec2 As Variant
dec1 = CDec( "1,001.0000001" )
' dec1 is now equal to the Decimal 1001.0000001
dec2 = CDec( 5 / 3 )
' 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.

VBA CDec Function Error

If the CDec function is supplied with a text string that cannot be converted into a numeric value, it will return the error:

Run-time error '13': Type mismatch

VBA Run Time Error 13 Message Box