如何找到VBA中的日期之间的差异 [英] How to find the difference between dates in VBA
问题描述
我正在尝试找出系统日期和工作表中存储的日期之间的区别。如果它们之间的差异> 30天,结果为真,否则结果为false
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.
推荐答案
我不知道为什么我很少看到使用日期功能的人。
I wonder why I rarely see people using the date functions.
您还可以使用以下内容:
You can also use this:
if DateDiff("d", date1, date2) > 30 then
在这种情况下,date1将是CDate(Worksheets(dates)。 ,1))
和date2将是sdate(或者以CDate的形式转换为日期,Jeff表示。
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表示我们是得到差异的日子,这是VBA中的年,月等的间隔:
"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
这篇关于如何找到VBA中的日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!