如何在 Excel VBA 中比较两个日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23925371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I compare two dates in Excel VBA?
提问by user3686056
I'm working on a school project to compare if date1 is equal to date2 - 1 (date1 is one day earlier than date2).
我正在做一个学校项目来比较 date1 是否等于 date2 - 1(date1 比 date2 早一天)。
date2 is located one cell below date1. This is will be placed in an if/else statement where the comparison will return a Boolean.
date2 位于 date1 下一个单元格。这将放置在 if/else 语句中,比较将返回一个布尔值。
This is the code that I am working on,
这是我正在处理的代码,
Sub someLoop()
Dim night As Long
night = 1
Dim c As Long
Dim max_rows As Long
max_rows = UsedRange.Rows.Count
For c = 2 To max_rows
Range("A" & c).Select
If ActiveCell.Value = ActiveCell.Offset(1, 0).Value2 - 1 Then
night = night + 1
Else
ActiveCell.Offset(0, 2).SetValue = night
night = 1
End If
Next c
End Sub
回答by skiingflea
Dates in Excel can be treated just like numbers.
Excel 中的日期可以像数字一样处理。
So, if [A1] has 28-May-14 and [A2] has 29-May-14, then you can just write the formula: =(A1=A2-1)
所以,如果 [A1] 有 28-May-14 而 [A2] 有 29-May-14,那么你可以写公式:=(A1=A2-1)