0% found this document useful (0 votes)
79 views

Difference Between Datevalue and Cdate in Vba: 3 Answers

This document discusses the difference between the DateValue and CDate functions in VBA. DateValue returns only the date portion of a date/time string, while CDate preserves both date and time. DateValue only accepts string parameters, whereas CDate can also handle numbers by converting them to dates. Both functions convert values to the date format of dd/mm/yyyy when used in VBA, but DateValue returns a serial number when used in a spreadsheet.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Difference Between Datevalue and Cdate in Vba: 3 Answers

This document discusses the difference between the DateValue and CDate functions in VBA. DateValue returns only the date portion of a date/time string, while CDate preserves both date and time. DateValue only accepts string parameters, whereas CDate can also handle numbers by converting them to dates. Both functions convert values to the date format of dd/mm/yyyy when used in VBA, but DateValue returns a serial number when used in a spreadsheet.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

21/3/2021 excel - Difference between DateValue and CDate in VBA - Stack Overflow

Difference between DateValue and CDate in VBA


Asked 6 years, 8 months ago Active 5 years, 3 months ago Viewed 57k times

I am new to VBA and I am working on a module to read in data from a spreadsheet and
calculate values based on dates from the spreadsheet. I read in the variables as a String and
14 then am currently changing the values to a Date using CDate. However I just ran across
DateValue and I was wondering what the difference between the two functions were and
which one is the better one to use.

5 vba excel

Share Improve this question edited Jul 9 '18 at 19:34 asked Jul 24 '14 at 21:54
Follow Community ♦ Ryan Newman
1 1 796 3 13 32

3 Answers Active Oldest Votes

DateValue will return only the date. CDate will preserve the date and time:

32 ? DateValue("2014-07-24 15:43:06")
24/07/2014

? CDate("2014-07-24 15:43:06")
24/07/2014 15:43:06

Similarly you can use TimeValue to return only the time portion:

? TimeValue("2014-07-24 15:43:06")
15:43:06

? TimeValue("2014-07-24 15:43:06") > TimeSerial(12, 0, 0)


True

Also, as guitarthrower says, DateValue (and TimeValue ) will only accept String parameters,
while CDate can handle numbers as well. To emulate these functions for numeric types, use
CDate(Int(num)) and CDate(num - Int(num)) .

Share Improve this answer edited Dec 14 '15 at 16:27 answered Jul 24 '14 at 22:44
Follow Chel
2,483 16 23

CDate will convert a number or text to a date.

Join Stack Overflow to learn, share knowledge, and build your career. Sign up
5
https://stackoverflow.com/questions/24944662/difference-between-datevalue-and-cdate-in-vba 1/2
21/3/2021 excel - Difference between DateValue and CDate in VBA - Stack Overflow

CDate(41035) 'Converts to a date of 5/6/2012


CDate("1/15/2014") 'Converts to a date of 1/15/2014

DateValue will convert date in text format (only) to a date.

DateValue("1/15/2014") 'Converts to a date of 1/15/2014


DateValue(41035) 'Throws a mismatch error

Share Improve this answer Follow answered Jul 24 '14 at 22:04


guitarthrower
5,267 2 26 36

Both CDate and DateValue, when used in vba, converts a value to a Date (dd/mm/yyyy
format):
3 1)
LstrDate = "July 24, 2014"
LDate = CDate(LstrDate)

2)
LDate = DateValue("July 24, 2014")

Both return the same result.

However DateValue returns the serial number of a date if used in application level
(spreadsheet)

Share Improve this answer edited Dec 21 '15 at 19:51 answered Jul 24 '14 at 22:06
Follow Ryan Newman Alex
796 3 13 32 1,592 1 11 24

2 didn't know about the spreadsheet function, difference. Good to know. – guitarthrower Jul 24 '14 at
22:35

Join Stack Overflow to learn, share knowledge, and build your career. Sign up

https://stackoverflow.com/questions/24944662/difference-between-datevalue-and-cdate-in-vba 2/2

You might also like