【发布时间】:2014-12-11 10:17:02
【问题描述】:
我有一列格式为“dd/mm/yy HH:MM:SS”的单元格,我想根据时间计算日期和时间在以下范围内的单元格的数量天。
如果当前时间
Count Occurances in range between 07:00 the previous day and now
否则
Count occurances between 07:00 today and now
如果结束
提前谢谢你。
【问题讨论】:
我有一列格式为“dd/mm/yy HH:MM:SS”的单元格,我想根据时间计算日期和时间在以下范围内的单元格的数量天。
如果当前时间
Count Occurances in range between 07:00 the previous day and now
否则
Count occurances between 07:00 today and now
如果结束
提前谢谢你。
【问题讨论】:
此代码很可能适合您。它也非常简单且易于更改以引用您需要查看的正确工作表和单元格。我希望这会有所帮助!
Sub CountingNumberOfOccurancesWithingRangeDependingOnTime()
Dim curTime As Date
Dim preTime As Date
Dim todayTime As Date
Dim curHour As Integer
Dim counter As Integer
curTime = Now
curHour = Format(curTime, "HH")
preTime = curTime - 1
x = 1
If curHour < 7 Then
Do Until Sheet1.Cells(x, 1).Value = Empty
cellTime = Sheet1.Cells(x, 1).Value
If cellTime > preTime And cellTime < curTime Then
counter = counter + 1
End If
x = x + 1
Loop
Else
todayTime = Date & " " & "07:00:00"
Do Until Sheet1.Cells(x, 1).Value = Empty
cellTime = Sheet1.Cells(x, 1).Value
If cellTime > todayTime And cellTime < curTime Then
counter = counter + 1
End If
x = x + 1
Loop
End If
MsgBox counter 'This isn't necissary, but it just is here to show the number of cells counted
End Sub
【讨论】: