2

午前8時から午後4時まで毎分実行されるスプレッドシートがあり、特定のフォルダーからファイルをインポートして特定の計算を実行します

計算は、制限に違反していないかどうかを確認するために行われます。制限に違反した場合、スプレッドシートは音声アラートと電子メールアラートを介してユーザーに通知する必要があります。要件は、音声アラートが1分ごとにオフになることですが、電子メールアラートは、電子メールでスパムされたくないため、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

2 に答える 2

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

(これは、最初に呼び出されたときにも送信されます)

于 2012-10-17T15:55:17.310 に答える
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からのものです。

幸運を。

于 2012-10-17T18:15:23.143 に答える