How to find the difference between dates in VBA

前端 未结 3 1622
一生所求
一生所求 2020-12-20 15:43

I am trying to find out the difference between the system date and the date stored in the worksheet. If the difference between them is > 30 days, the result is true, else th

3条回答
  •  爱一瞬间的悲伤
    2020-12-20 16:02

    I wonder why I rarely see people using the date functions.

    You can also use this:

    if DateDiff("d", date1, date2) > 30 then
    

    in this case, date1 would be CDate(Worksheets("dates").Cells(1,1)) and date2 would be sdate (either cast with CDate or dim'd as a date as Jeff said.

    "d" means we are getting the difference in days. Here are the intervals for years, months, etc. in VBA:

    yyyy - Year
    q - Quarter
    m - Month
    y - Day of year
    d - Day
    w - Weekday
    ww - Week
    h - Hour
    n - Minute
    s - Second
    

提交回复
热议问题