The VBA DateValue Function returns a VBA Date from a supplied String representation of a date. Time information within the supplied string is ignored.
The syntax of the function is:
Where the Date argument is a valid String representation of date/time.
The DateValue function can interpret text representations of dates that are in a recognised Excel format. However, the function is unable to interpret dates that include the text description of the weekday (Sunday, Monday, etc).
' Convert two supplied strings into dates
Dim dt1 As Date
Dim dt2 As Date
dt1 = DateValue( "12/31/2015" )dt2 = DateValue( "Jan 1 2016 3:00 AM" )
' dt1 is now equal to the date 12/31/2015
' dt2 is now equal to the date 1/1/2016
In the above VBA code:
The DateValue function converts the text string "Jan 1 2016 3:00 AM" into the VBA date 1/1/2016.
If the Date that is supplied to the DateValue function cannot be converted to a valid date, you will get the error: