嗨,我正在尝试使用vba创建宏 一个例子是
DATETIMEA is "20/11/2018 11:00:00 AM"
DATETIMEB is "20/11/2018 08:00:00 AM"
DATETIME is "21/11/2018 07:59:00 AM"
我想检查datetimeA
是否在datetimeb
和datetimec
之间,以下代码不起作用。
这些值存储在Date
变量中。
If datetimeb >= datetime And datetimea <= datetimec
答案 0 :(得分:0)
目前还不清楚如何为变量分配日期,所以我假设您已经完成了该部分。
这里有两种检查语句的方法:DateB >= DateA >= DateC
Sub DateCompare()
Dim DateA As Date 'Variable Date
Dim DateB As Date 'Older Date
Dim DateC As Date 'Newer Date
DateA = Some Date
DateB = Some Date
DateC = Some Date
'Method to give Boolean Response
MsgBox DateA >= DateB And DateA <= DateC
'Method to do other things
If DateA >= DateB And DateA <= DateC Then
MsgBox "DateA falls in-between DateB & DateC"
Else
MsgBox "DateA does not fall in-between DateB & DateC"
End If
End Sub
这可以直接在Excel中完成,而无需VBA
...
A2
=可变日期
B2
=较早的日期
C2
=更新日期
= AND(A2> = B2,A2 <= C2)
该方程式将返回TRUE
或FALSE
。如果您想返回非布尔型响应,则方程可以嵌套在IF
语句中
= IF(AND(A2> = B2,A2 <= C2)= TRUE,“ A在C&B之间”,“ A不在C&B之间”)