我有一个电子表格,每分钟在上午8点到下午4点之间运行,从某个文件夹导入文件并进行某些计算
进行计算以检查是否违反了限制。如果违反限制,则电子表格需要通过声音警报和电子邮件警报通知用户。要求是每隔一分钟发出声音警报,而电子邮件警报应该每45分钟一次,因为他们不想通过电子邮件发送垃圾邮件。
public sub fileImport()
if between 0800 to 1600
//do file import
//do calculations
if breach sound alert
end if
Application.OnTime RunEveryMinute, fileImport
if there is a breach
sendMail()
Application.OnTime RunEvery45Min, sendMail
end if
public sub sendEmail()
//do email function
那么我怎样才能每隔45分钟而不是每分钟调用一次sendEmail子?
答案 0 :(得分:4)
假设这是一个循环,只记录上一个发送时间并将其与Now
进行比较;
Public Sub sendEmail()
Static lastSent As Date
If DateDiff("m", lastSent, Now) > 45 Then
'//do email function
lastSent = Now
End If
End Sub
(这也是第一次调用它)
答案 1 :(得分:0)
您可以使用OnTime功能:
Public RunWhen As Double
Public Const cRunIntervalSeconds = 120 ' two minutes
Public Const cRunWhat = "TheSub"
Sub StartTimer()
RunWhen = Now + TimeSerial(0,0,cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
Schedule:=True
End Sub
'This stores the time to run the procedure in the variable RunWhen, two minutes after the current time.
'Next, you need to write the procedure that will be called by OnTime. For example,
Sub TheSub()
' Put your send email code here.
StartTimer ' Reschedule the procedure
End Sub
此示例来自:http://www.cpearson.com/excel/OnTime.aspx
祝你好运。