VBA CDate Function

VBA CDate Function

Objective of VBA CDate Function:


VBA CDate Function is categorized as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CDate function converts an expression to a Date data type.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CDate 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 CDate function, where we can use this VBA CDate Function and it’s real-time examples.

Syntax of VBA CDate Function:

The syntax of the CDate Function in VBA is

CDate(Expression)

The CDate function returns a date data type value.

Parameters or Arguments:

The CDate function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a Date or Time value. It is used to convert the string or numeric value to a date value.

Where we can apply or use VBA CDate Function?

We can use this CDate 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(Sep 21, 2018) to Date Data Type

Here is a simple example of the VBA CDate function. This below example converts an expression(Sep 21, 2018) to Date data type.

'Convert a String(Sep 21, 2018) to Date Data Type
Sub VBA_CDate_Function_Ex1()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Date
    
    sValue = "Sep 21, 2018"
        
    dResult = CDate(sValue)
        
    MsgBox "String to Date Data Type : " & dResult, vbInformation, "VBA CDate Function"
    
End Sub

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

Example 2: Convert a Value(21/12 12:12) to Date Data Type

Here is a simple example of the VBA CDate function. This below example converts an expression(21/12 12:12) to Date data type.

'Convert a Value(21/12 12:12) to Date Data Type
Sub VBA_CDate_Function_Ex2()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Date
    
    sValue = "21/12 12:12"
        
    dResult = CDate(sValue)
        
    MsgBox "String to Date Data Type : " & dResult, vbInformation, "VBA CDate Function"
    
End Sub

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

Example 3: Convert a Value(12:21) to Date Data Type

Here is a simple example of the VBA CDate function. This below example converts an expression(12:21) to Date data type.

'Convert a Value(12:21) to Date Data Type
Sub VBA_CDate_Function_Ex3()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Date
    
    sValue = "12:21"
        
    dResult = CDate(sValue)
        
    MsgBox "String to Date Data Type : " & dResult, vbInformation, "VBA CDate Function"
    
End Sub

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

Example 4: Convert a Value(43214) to Date Data Type

Here is a simple example of the VBA CDate function. This below example converts an expression(43214) to Date data type.

'Convert a Value(43214) to Date Data Type
Sub VBA_CDate_Function_Ex4()

    'Variable declaration
    Dim sValue As String
    Dim dResult As Date
    
    sValue = 43214
        
    dResult = CDate(sValue)
        
    MsgBox "String to Date Data Type : " & dResult, vbInformation, "VBA CDate Function"
End Sub

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

Example 5: Convert a String(sunday) to Date Data Type(Returns an Error)

Here is a simple example of the VBA CDate function. This below example converts an expression(sunday) to Date data type.

'Convert a String(sunday) to Date Data Type(Returns an Error)
Sub VBA_CDate_Function_Ex5()

    'Variable declaration
    Dim sValue As Integer
    Dim dResult As Date
    
    sValue = "sunday"
        
    dResult = CDate(sValue)
        
    MsgBox "String to Date Data Type : " & dResult, vbInformation, "VBA CDate Function"
End Sub

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

Instructions to use Macro Codes

Here are the step by step instructions to use the CDate Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA Data Type Conversion Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *