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 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.
相关问题
- Excel sunburst chart: Some labels missing
- Error handling only works once
- Error handling only works once
- Excel formula in VBA code
- Excel VBA run time error 450 from referencing a ra
相关文章
- Get column data by Column name and sheet name
- programmatically excel cells to be auto fit width
- Unregister a XLL in Excel (VBA)
- Unregister a XLL in Excel (VBA)
- How to prevent excel from truncating numbers in a
- numeric up down control in vba
- Declare a Range relative to the Active Cell with V
- What's the easiest way to create an Excel tabl
DateValue
will return only the date.CDate
will preserve the date and time:Similarly you can use
TimeValue
to return only the time portion:Also, as guitarthrower says,
DateValue
(andTimeValue
) will only acceptString
parameters, whileCDate
can handle numbers as well. To emulate these functions for numeric types, useCDate(Int(num))
andCDate(num - Int(num))
.CDate
will convert a number or text to a date.DateValue
will convert date in text format (only) to a date.Both CDate and DateValue, when used in vba, converts a value to a Date (dd/mm/yyyy format):
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)