问题描述
我有一个看起来像这样的表:
I have a table which looks like this:
我要做的是汇总给定日期时间范围内的一些值。假设在01.01.2016 00:30到01.01.2016 02:00之间。
What I am trying to do is to sum up a few values within a given datetime range. Let's say for example between 01.01.2016 00:30 and 01.01.2016 02:00.
我的if子句看起来像这样。
My if clause looks like this.
If .Cells(i, 3).Value = "Samariter" And .Cells(i, 1).Value >= "01.01.2016 00:30:00" And .Cells(i, 1).Value <= "01.01.2016 02:00:00" Then
如果我运行算法,则算法可以正确运行,但是:
And if I run the algorithm, it runs correctly, BUT:
例如,如果我尝试使用从25.1.2016 20:00到26.1.2016 02:00:
If I try, for example, the range from 25.1.2016 20:00 until 26.1.2016 02:00:
此再次是我的if子句:
This is my if clause again:
If .Cells(i, 3).Value = "Samariter" And .Cells(i, 1).Value >= "25.01.2016 20:00:00" And .Cells(i, 1).Value <= "26.01.2016 02:00:00" Then
如果运行此命令,它将从正确的行开始,但将超过datetime interva的上限l。
If I run this, it will start at the correct line, but will exceed the upper bound of the datetime interval.
这里是什么问题?
推荐答案
Xabier的答案解决了问题,if子句应该看起来像这样才能使算法正常运行:
Xabier's answer solved the problem, the if-clause should look like this in order for the algorithm to run properly:
If .Cells(i, 3).Value = "Samariter" And DateDiff("s", .Cells(i, 1).Value, "25.01.2016 00:00:00") < 0 And DateDiff("s", .Cells(i, 1).Value, "31.01.2016 23:59:59") >= 0 Then
这篇关于检查日期时间是否在给定的日期时间间隔之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!