VBA CDec Function

VBA CDec Function in Excel

VBA CDec Function in Excel is categorizes as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CDec function converts an expression to a Decimal data sub-type.

This function use in either procedure or function in a VBA editor window in Excel. We can use this VBA CDec Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CDec function, where we can use this VBA CDec Function and it’s real-time examples.

Syntax of VBA CDec Function:

The syntax of the CDec Function in VBA is

CDec(Expression)

The CDec function returns a decimal data type value.

Parameters or Arguments:

The CDec function has one argument in Excel VBA.
where Expression: It is a mandatory argument. An expression argument represents a value. It is used to convert the value to a decimal value.

Where we can apply or use VBA CDec Function?

We can use this CDec Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Convert a String(12345.12345) to Decimal Data Type

Here is a simple example of the VBA CDec function. This below example converts given expression(12345.12345) to decimal type.

'Convert a String(12345.12345) to Decimal Data Type
Sub VBA_CDec_Function_Ex1()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Double
    
    sValue = 12345.12345
        
    dResult = CDec(sValue)
        
    MsgBox "String(12345.12345) to Decimal Data Type : " & dResult, vbInformation, "VBA CDec Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA CDec Function

Example 2: Convert a Value(24/5) to Decimal Data Type

Here is a simple example of the VBA CDec function. This below example converts given expression(24/5) to decimal type.

'Convert a Value(24/5) to Decimal Data Type
Sub VBA_CDec_Function_Ex2()

    'Variable declaration
    Dim sValue
    Dim dResult As Double
    
    sValue = 24 / 5
        
    dResult = CDec(sValue)
        
    MsgBox "Value(24/5) to Decimal Data Type : " & dResult, vbInformation, "VBA CDec Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA CDec Function

Example 3: Convert a Value(10000000000.5678) to Decimal Data Type

Here is a simple example of the VBA CDec function. This below example converts given expression(10000000000.5678) to decimal type.

'Convert a Value(10000000000.5678) to Decimal Data Type
Sub VBA_CDec_Function_Ex3()

    'Variable declaration
    Dim sValue As Currency
    Dim dResult As Double
    
    sValue = 10000000000.5678
        
    dResult = CDec(sValue)
        
    MsgBox "Value(10000000000.5678) to Decimal Data Type : " & dResult, vbInformation, "VBA CDec Function"
End Sub

Output: Here is the screen shot of the third example output.
VBA CDec Function

Example 4: Convert a Value(1.02*1.03*1.04) to Decimal Data Type

Here is a simple example of the VBA CDec function. This below example converts given expression(1.02*1.03*1.04) to decimal type.

'Convert a Value(1.02*1.03*1.04) to Decimal Data Type
Sub VBA_CDec_Function_Ex4()

    'Variable declaration
    Dim dResult As Double
    
    sValue1 = 1.02: sValue2 = 1.03: sValue3 = 1.04
        
    dResult = CDec(sValue1 * sValue2 * sValue3)
        
    MsgBox "Value(1.02*1.03*1.04) to Decimal Data Type : " & dResult, vbInformation, "VBA CDec Function"
    
End Sub

Output: Here is the screen shot of the fourth example output.
VBA CDec Function

Example 5: Convert a string(Test) to Decimal Data Type

Here is a simple example of the VBA CDec function. This below example converts given expression(Test) to decimal type. It returns an error as type mismatch. Because, it can’t convert given string to decimal data type.

'Convert a string(Test) to Decimal Data Type
Sub VBA_CDec_Function_Ex5()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Double
    
    sValue = "Test"
        
    dResult = CDec(sValue)
        
    MsgBox "String(Test) to Decimal Data Type : " & dResult, vbInformation, "VBA CDec Function"
    
End Sub

Output: Here is the screen shot of the fifth example output.
VBA CDec Function

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply