Excel逻辑问题

我已经开发了一个简单的vba函数来确定是否有人在下class后工作。 这是我写的原始查询:

Public Function AfterHours(StartTime As Date, EndTime As Date, ActivityDate As Date) AfterHours = False If (Weekday(ActivityDate) <> 1 OR Weekday(ActivityDate) <> 7) Then If Hour(StartTime) >= 19 Or Hour(EndTime) < 7 Then AfterHours = True End If Else AfterHours = True End If End Function 

但是,这个查询并不会在正常工作时间内接收周末工作的员工。 如果我将其更改为:

 Public Function AfterHours(StartTime As Date, EndTime As Date, ActivityDate As Date) AfterHours = False If (Weekday(ActivityDate) = 1 Or Weekday(ActivityDate) = 7) Then AfterHours = True Else If Hour(StartTime) >= 19 Or Hour(EndTime) < 7 Then AfterHours = True End If End If End Function 

查询function正常。 两者的逻辑是相同的,只是倒过来。 在第一个function,如果不是周末,它应该testing是否在营业时间以外,否则是周末,应该被标记。 第二个函数检查是否是周末,并标记它,否则检查是否在营业时间以外。

我不明白为什么这些查询返回不同的结果。

你的第一个If陈述是错误的。

 If (Weekday(ActivityDate) <> 1 OR Weekday(ActivityDate) <> 7) Then 

因为你使用Or这将永远是真实的。 使用, And不是。