Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 检查时间是否在不工作的范围内_Vba_Excel_Datetime - Fatal编程技术网

Vba 检查时间是否在不工作的范围内

Vba 检查时间是否在不工作的范围内,vba,excel,datetime,Vba,Excel,Datetime,我需要检查时间是否在下午3:31:00到早上6:29:00之间 这是调试时立即窗口中返回的内容 ?TimeValue(TEMP.Cells(i, tAssgnStart)) >= WorksheetFunction.Min(TimeValue("15:31:00"), TimeValue("06:29:00")) False ?TimeValue(TEMP.Cells(i, tAssgnStart)) 02:00:00 ?WorksheetFunction.Min(TimeValue("

我需要检查时间是否在下午3:31:00到早上6:29:00之间

这是调试时立即窗口中返回的内容

?TimeValue(TEMP.Cells(i, tAssgnStart)) >= WorksheetFunction.Min(TimeValue("15:31:00"), TimeValue("06:29:00"))
False
?TimeValue(TEMP.Cells(i, tAssgnStart))
02:00:00 
?WorksheetFunction.Min(TimeValue("15:31:00"), TimeValue("06:29:00"))
 0.270138888888889 
?cdate(WorksheetFunction.Min(TimeValue("15:31:00"), TimeValue("06:29:00")))
06:29:00 

?TimeValue(TEMP.Cells(i, tAssgnStart)) <= WorksheetFunction.Max(TimeValue("15:31:00"), TimeValue("06:29:00"))
True
?TimeValue(TEMP.Cells(i, tAssgnStart))
02:00:00 
?WorksheetFunction.Max(TimeValue("15:31:00"), TimeValue("06:29:00"))
 0.646527777777778 
?cdate(WorksheetFunction.Max(TimeValue("15:31:00"), TimeValue("06:29:00")))
15:31:00 
?TimeValue(临时单元格(i,tAssgnStart))>=工作表function.Min(TimeValue(“15:31:00”)、TimeValue(“06:29:00”))
假的
?时间值(温度单元(i、tAssgnStart))
02:00:00 
?工作表function.Min(时间值(“15:31:00”)、时间值(“06:29:00”))
0.270138888888889
?cdate(工作表function.Min(时间值(“15:31:00”)、时间值(“06:29:00”))
06:29:00 

?时间值(临时单元格(i,tAssgnStart))您的方法在概念上是错误的。在Excel(VBA)中,一天是一个整数。Print CLng(Date)将给出今天的整数。明天就是今天+1。今天00:00开始,24:00结束。因此,每小时等于1/24,每分钟等于1/24/60,每秒钟等于1/24/60/60,依此类推。时间是一个双倍值,可以非常精确。更粗略地说,43028.5是代表2017年10月20日中午12点的日期/时间值

显然,15:31(0.646527778)比06:29(0.27013888889)晚。如果您希望指定明天的06:29,则必须添加一天,例如1.27013888889或43029.2701388888889。确定时间是否在43028.64652777778和43029.2701388888888889之间不会对您造成问题,还是会


打印CDbl(现在)将为您提供当前日期和时间的日期/时间值。Cdbl(时间值(“15:31”)将以1的分数返回时间值。

Variatus提供了时间和日期值在Excel中存储方式的良好概述。为了只比较一些任意值的时间值,我会做如下的事情,其中减去您使用的值的整数部分将确保只比较变量的时间部分

Sub Tester()

    Dim t, x
    For x = 1 To 23
        t = TimeValue(Format(x, "00") & ":00:00")
        Debug.Print Format(t, "hh:mm:ss"), t > TimeValue("15:31:00") Or _
                                           t < TimeValue("06:29:00")
    Next x

End Sub
Function isBetweenTimes(fromTime As Date, toTime As Date, timeToCheck As Date) As Boolean
  Dim f As Double, t As Double, c As Double

  f = CDbl(fromTime): f = f - Int(f)
  t = CDbl(toTime): t = t - Int(t)
  c = CDbl(timeToCheck): c = c - Int(c)

  isBetweenTimes = CBool(c >= f And c <= t)
End Function

Sub testFunction()
  Dim fromTime As Date, toTime As Date, timeToCheck As Date

  fromTime = #6:29:00 AM#
  toTime = #3:31:00 PM#
  timeToCheck = #2:00:00 PM#

  Debug.Print isBetweenTimes(fromTime, toTime, timeToCheck)
End Sub
函数的时间间隔(fromTime As Date、toTime As Date、timeToCheck As Date)为布尔值
尺寸f为双精度,t为双精度,c为双精度
f=CDbl(fromTime):f=f-Int(f)
t=CDbl(toTime):t=t-Int(t)
c=CDbl(timeToCheck):c=c-Int(c)

IsBetweentTimes=CBool(c>=f和c,但老实说,这似乎有点太费劲了。蒂姆的回答有什么问题吗?@Rohan嗯,我不喜欢把日期当作字符串处理,这就是
timevalue
所做的。如果你把它们当作实际的日期值来处理,你就不会有太多的机会,比如说,格式化导致问题。此外,他的方法只是这是一堆违反stackoverflow原则的代码,公平地说,这个问题并没有那么复杂,但我仍然觉得至少有一个简单的解释可以解释为什么要在答案中这样做。@Rohan这个答案中显示的函数也可以插入到你想在其中使用的任何未来宏中,并且它的功能相当自我描述——阅读Tim答案中的代码并不是那么容易。关于他没有声明变量类型之类的问题,我也有一些小的挑剔,但这些更多的是关于良好的编码标准,而不是其他任何东西:P
Function isBetweenTimes(fromTime As Date, toTime As Date, timeToCheck As Date) As Boolean
  Dim f As Double, t As Double, c As Double

  f = CDbl(fromTime): f = f - Int(f)
  t = CDbl(toTime): t = t - Int(t)
  c = CDbl(timeToCheck): c = c - Int(c)

  isBetweenTimes = CBool(c >= f And c <= t)
End Function

Sub testFunction()
  Dim fromTime As Date, toTime As Date, timeToCheck As Date

  fromTime = #6:29:00 AM#
  toTime = #3:31:00 PM#
  timeToCheck = #2:00:00 PM#

  Debug.Print isBetweenTimes(fromTime, toTime, timeToCheck)
End Sub