The VBA CDATE function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it converts an expression into the date data type. In simple words, with date data type it can hold a value that includes date and time as per VBA’s valid date and time system.
Syntax
CDate(Expression)
Arguments
- Expression: The value which you want to convert to the date data type.
Example
To practically understand how to use the VBA CDATE function, you need to go through the below example where we have written a vba code by using it:
Sub example_CDATE()
Range("B1").Value = CDate(Range("A1"))
End Sub
In the above example, we used the value from cell A1, where we have a number (8.5694), and then we used the CDATE function to convert that value into date data type, and it returned 7/8/2018 0:00 as the result.
Notes
- It can interpret text representations of dates and times that VBA recognizes as Excel date and time formats.
- A value that can’t be converted as date and time, VBA returns the run-time 13 error.
- Back to the Excel VBA / VBA Functions