2012-10-17 32 views
2

我有00-08:00之間每分鐘運行,至下午四時,從特定的文件夾導入文件,並做了電子表格某些計算宏運行的每一分鐘,但我想要一個名分,以每45分鐘跑

計算是做檢查是否違反了限制。如果違反限制,則電子表格需要通過聲音警報和電子郵件警報通知用戶。要求聲音警報每分鐘關閉,而電子郵件警報應該每隔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 

所以我怎麼能只調用sendEmail子每45分鐘而不是每分鐘?

回答

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 

(這將發送的第一次它被調用過)

0

您可以只使用導通時間功能:

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

好運。