[英]Excel VBA - Weekday and/or datediff function not working properly
I have the code below which is supposed to parse a column with dates/times and advise (by highlighting) if the value is older than 24 hours ago as of 6:30am on the current day. 我有下面的代码应该用日期/时间解析一列,并建议(通过突出显示)该值是否超过24小时前的当天早上6:30。 If the day is Monday, it is supposed to look back 72 hours (since Saturday and Sunday are not work days) and advise if the value is older than 72 hours as of 6:30am. 如果当天是星期一,则应该回顾72小时(因为星期六和星期日不是工作日),并建议从早上6:30开始,该值是否超过72小时。
Something is amiss as it is not highlighting values that are breaching the 24 hours threshold, but I don't know what it is. 有些东西是不对的,因为它没有突出显示违反24小时门槛的价值,但我不知道它是什么。
'Highlight breached tickets
Dim updateRange As Range, updateCell As Range
Set updateRange = Range("D2:D" & lastRow)
If Weekday(Date, vbMonday) Then
For Each updateCell In updateRange
If DateDiff("h", CDate(updateCell.Value), CDate(Format(Now(), "mm/dd/yy")) + TimeSerial(6, 30, 0)) > 72 Then
updateCell.Interior.Color = 13311
updateCell.Offset(0, -3).Interior.Color = 13311
End If
Next updateCell
Else
For Each updateCell In updateRange
If DateDiff("h", CDate(updateCell.Value), CDate(Format(Now(), "mm/dd/yy")) + TimeSerial(6, 30, 0)) > 24 Then
updateCell.Interior.Color = 13311
updateCell.Offset(0, -3).Interior.Color = 13311
End If
Next updateCell
End If
As others have said you need to test Weekday
against something as it doesn't return a Boolean
value. 正如其他人说你需要测试Weekday
反对的东西,因为它不返回一个Boolean
值。 However, thought I'd add this as you could re-write your code so you don't repeat yourself as nearly all of it is doing exactly the same 但是,我想我会添加这个,因为你可以重新编写你的代码,所以你不要重复自己,因为几乎所有的代码完全相同
'Highlight breached tickets
Dim updateRange As Range, updateCell As Range
Dim TimeDiff As Long
Set updateRange = Range("D2:D" & Lastrow)
TimeDiff = IIf(Weekday(Date, vbMonday) = 1, 72, 40)
For Each updateCell In updateRange
If DateDiff("h", CDate(updateCell.Value), CDate(Format(Now(), "mm/dd/yy")) + TimeSerial(6, 30, 0)) > TimeDiff Then
updateCell.Interior.Color = 13311
updateCell.Offset(0, -3).Interior.Color = 13311
End If
Next updateCell
Writing it this way makes it much easier to maintain 以这种方式编写它使维护更容易
Update after comments To modify this to cope with other days as well, the simplest (and easiest to read) would be to change the TimeDiff =
line and using a Select Case
statement to set the value 注释后更新要修改它以应对其他日子,最简单(也是最容易阅读)的方法是更改TimeDiff =
行并使用Select Case
语句设置值
'Highlight breached tickets
Dim updateRange As Range, updateCell As Range
Dim TimeDiff As Long
Set updateRange = Range("D2:D" & Lastrow)
Select Case Weekday(Date, vbMonday)
' Monday
Case 1
TimeDiff = 72
' Tuesday
Case 2
TimeDiff = 96
' Any other day
Case Else
TimeDiff = 40
End Select
For Each updateCell In updateRange
If DateDiff("h", CDate(updateCell.Value), CDate(Format(Now(), "mm/dd/yy")) + TimeSerial(6, 30, 0)) > TimeDiff Then
updateCell.Interior.Color = 13311
updateCell.Offset(0, -3).Interior.Color = 13311
End If
Next updateCell
As mentioned by Kostas K. in the comments, Weekday()
returns an Integer
number from 1 to 7, which is always evaluated to a True
boolean - MSDN Weekday ! 正如Kostas K.在评论中所提到的, Weekday()
返回一个从1到7的Integer
,它总是被计算为一个True
布尔值 - MSDN Weekday !
However, if you use If Weekday(Date, vbMonday) = vbMonday
most probably you will get a False
if the today is Monday. 但是,如果您使用If Weekday(Date, vbMonday) = vbMonday
如果今天是星期一,则很可能会得到一个False
。
That is because the default day of the beginning of the Weekday is Sunday. 这是因为工作日开始的默认日是星期日。 And you are changing it to Monday. 而你正在改变它到星期一。 And from the contrary, vbMonday
is always evaluated to 2
(write ?vbMonday
at the immediate window) 而相反, vbMonday
总是被评估为2
(在即时窗口写入?vbMonday
)
Anyhow, it is probably a bit easier to see it than to explain it. 无论如何,看到它可能比解释它要容易一些。 Today it is Friday (TGIF). 今天是星期五(TGIF)。 Thus, simply run the following code: 因此,只需运行以下代码:
Public Sub TestMe()
Debug.Print Weekday(Date) = vbFriday 'True
Debug.Print Weekday(Date, vbMonday) = vbFriday 'False
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.