macros运行每分钟,但我想要一个特定的子每45分钟运行一次

我有一个电子表格,上午8点到下午4点每分钟运行一次,从某个文件夹导入文件并进行一定的计算

计算是为了检查是否违反了限制。 如果违反限制,则电子表格需要通过声音警报和电子邮件警报通知用户。 要求每分钟发出一次声音警报,而电子邮件警报应该每隔45分钟closures一次,因为他们不希望被电子邮件垃圾邮件。

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子?

假设这是在一个循环中,只logging上次发送时间并将其与Now进行比较;

 Public Sub sendEmail() Static lastSent As Date If DateDiff("m", lastSent, Now) > 45 Then '//do email function lastSent = Now End If End Sub 

(这也会第一次发送它所调用的)

你可以使用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

祝你好运。