Excel if-else循环内执行不正确

Excel if-else循环内执行不正确,excel,if-statement,vba,Excel,If Statement,Vba,我有以下代码。但是,if-else循环执行不正确。即MsgBox(小时(标准时间))打印14 因此If(Hour(strTime)=17分钟(time)=31周(time)=1周(time)和2周(time)应该不执行。但不管怎样,每次都是这样。如果有人看到代码中有任何错误,我们将不胜感激。致意 Private RunWhen As Date Private Const StartTime As Date = #2:14:30 PM# Private Const EndTime As Date

我有以下代码。但是,
if-else
循环执行不正确。即
MsgBox(小时(标准时间))
打印
14

因此
If(Hour(strTime)=17分钟(time)=31周(time)=1周(time)和2周(time)
应该不执行。但不管怎样,每次都是这样。如果有人看到代码中有任何错误,我们将不胜感激。致意

Private RunWhen As Date
Private Const StartTime As Date = #2:14:30 PM#
Private Const EndTime As Date = #5:34:55 PM#
Private Const cRunInterval = "00:01:00"
Private Const cRunWhat = "Data" ' the name of the procedure to run


Sub StartTimer()
Dim strTime
strTime = time

    If RunWhen = 0 Then
        RunWhen = StartTime
    Else
        RunWhen = RunWhen + TimeValue(cRunInterval)
    End If
    MsgBox (Hour(strTime))
    If (RunWhen <= EndTime & Weekday(time) <> 1 & Weekday(time) <> 2) Then
        If (Hour(strTime) = 17 & Minute(time) = 31 & Weekday(time) <> 1 & Weekday(time) <> 2) Then
            'MsgBox ("Mail")
            Call CDO_Mail_Small_Text
        Else
            If (Hour(strTime) = 17 & Minute(time) = 33 & Weekday(time) <> 1 & Weekday(time) <> 2) Then
                MsgBox ("clear")
                Call ClearData
            Else
            Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
            End If
        End If
    End If


End Sub

Sub Data()
Dim RowNo As Long
Dim RowNo2 As Long
RowNo = Sheets(1).Cells(Rows.Count, 11).End(xlUp).Row
RowNo2 = Sheets(1).Cells(Rows.Count, 14).End(xlUp).Row + 1
    Sheets(1).Cells(RowNo2, 15) = Sheets(1).Cells(RowNo, 11) 
    Sheets(1).Cells(RowNo2, 16) = Sheets(1).Cells(RowNo, 12) 
    Sheets(1).Cells(RowNo2, 17) = Sheets(1).Cells(RowNo, 13) 
    Sheets(1).Cells(RowNo2, 14) = time

    StartTimer ' Reschedule the procedure
End Sub
Private RunWhen As Date
私人施工开始时间日期=#下午2:14:30#
私人康斯特结束时间截止日期=#下午5:34:55#
私人Const cRunInterval=“00:01:00”
Private Const cRunWhat=“Data”'要运行的过程的名称
亚StartTimer()
暗时间
strTime=时间
如果RunWhen=0,则
运行时间=开始时间
其他的
运行时间=运行时间+时间值(cRunInterval)
如果结束
MsgBox(小时(标准时间))

If(RunWhen我认为您的问题在于使用
&
而不是布尔运算符

Sub StartTimer()
Dim strTime
strTime = time

If RunWhen = 0 Then
    RunWhen = StartTime
Else
    RunWhen = RunWhen + TimeValue(cRunInterval)
End If
MsgBox (Hour(strTime))
If (RunWhen <= EndTime AND Weekday(time) <> 1 AND Weekday(time) <> 2) Then
    If (Hour(strTime) = 17 AND Minute(time) = 31 AND Weekday(time) <> 1 AND Weekday(time) <> 2) Then
        'MsgBox ("Mail")
        Call CDO_Mail_Small_Text
    Else
        If (Hour(strTime) = 17 AND Minute(time) = 33 AND Weekday(time) <> 1 & Weekday(time) <> 2) Then
            MsgBox ("clear")
            Call ClearData
        Else
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
        End If
    End If
End If


End Sub
Sub-StartTimer()
暗时间
strTime=时间
如果RunWhen=0,则
运行时间=开始时间
其他的
运行时间=运行时间+时间值(cRunInterval)
如果结束
MsgBox(小时(标准时间))

If(RunWhen我认为您的问题在于使用
&
而不是布尔运算符

Sub StartTimer()
Dim strTime
strTime = time

If RunWhen = 0 Then
    RunWhen = StartTime
Else
    RunWhen = RunWhen + TimeValue(cRunInterval)
End If
MsgBox (Hour(strTime))
If (RunWhen <= EndTime AND Weekday(time) <> 1 AND Weekday(time) <> 2) Then
    If (Hour(strTime) = 17 AND Minute(time) = 31 AND Weekday(time) <> 1 AND Weekday(time) <> 2) Then
        'MsgBox ("Mail")
        Call CDO_Mail_Small_Text
    Else
        If (Hour(strTime) = 17 AND Minute(time) = 33 AND Weekday(time) <> 1 & Weekday(time) <> 2) Then
            MsgBox ("clear")
            Call ClearData
        Else
        Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
        End If
    End If
End If


End Sub
Sub-StartTimer()
暗时间
strTime=时间
如果RunWhen=0,则
运行时间=开始时间
其他的
运行时间=运行时间+时间值(cRunInterval)
如果结束
MsgBox(小时(标准时间))

如果(RunWhen谢谢,我一直在用很多javascript编程…忘记了VBA有
:P我现在没有任何未定义的错误,并且有显式选项。很高兴听到它的帮助。我完全理解切换语言的困难。我相信这会发生在我们当中最好的人身上!谢谢,我一直在用很多javascript编程…因为了解VBA有
:P我没有任何未定义的错误,现在有选项显式。很高兴听到它有帮助。我完全理解转换语言的困难。我相信这会发生在我们当中最好的人身上!