Difference between DateValue and CDate in VBA

后端 未结 3 699
暖寄归人
暖寄归人 2020-12-15 23:42

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

3条回答
  •  一生所求
    2020-12-16 00:08

    CDate will convert a number or text to a date.

    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
    

提交回复
热议问题