Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 the result is false

Dim result as boolean
Dim sDate as string
sDate = Date
if Worksheets("dates").Cells(1,1) - sDate > 30 then 'how do I do this?
     result = true
else
     result = false
end if

How do I find out the difference in days between the system date and the date stored in the worksheet? The date in the worksheet can be a past date, too.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
906 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...