我有以下代码。但是if-else
循环执行不正确。即MsgBox (Hour(strTime))
打印14
,
因此If (Hour(strTime) = 17 & Minute(time) = 31 & Weekday(time) <> 1 & Weekday(time) <> 2)
应该不执行。但它无论如何都会这样做。感谢有人看到代码中的任何错误。最诚挚的问候
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
答案 0 :(得分:3)
我相信您的问题是使用&
而不是布尔运算符AND
。
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,但是当我测试它时,我得到了许多未定义的变量。
优良作法是确保在模块的开头声明Option Explicit
。
这确保了所有变量的声明,并且它使我免于因拼写错误而追踪变量的无数个小时。