5

I want to monitor a value and get an email notifications when certain conditions are met. I have a macro like so:

Do While True

    Worksheet.Calculate

    If Value > 10 Then
        SendEmail
    End If

    Sleep 60*CLng(1000)

Loop

However, when I run this, it clogs up the entire program and will turn unresponsive if I try to do anything.

Is there anyway to accomplish this but have it run in the background or at least not crash the program?

What I was doing before was using VBScript to open a not-visible spreadsheet and the VBScript ran continuously in the background monitoring the condition and worked fine, but my client really wants a GUI and for it to be in the program itself.

Any thoughts?

3
  • 1
    If you need to monitor a cell's value than you don't need to continuously run a code, but use the worksheet's change event. Commented Jul 3, 2015 at 16:36
  • @MátéJuhász I don't think the value will change on its own. It needs to be prompted to. I'll update the sample code. Can I still use worksheet's change event? Commented Jul 3, 2015 at 16:47
  • Have a look here: mrexcel.com/forum/excel-questions/…, it might be useful for you. Commented Jul 3, 2015 at 17:01

2 Answers 2

9

Use the Application.OnTime method to schedule code that will run in one minute.

Your code will look something like this (Untested):

Sub CreateNewSchedule()
    Application.OnTime EarliestTime:=DateAdd("n", 1, Now), Procedure:="macro_name", Schedule:=True
End Sub

Sub macro_name()

    If Value > 10 Then
        SendEmail
    Else
        CreateNewSchedule
    End If

End Sub

You might want to store the time of the next schedule in a global variable so that the Workbook_BeforeClose event can cancel the next schedule. Otherwise Excel will re-open the workbook.

Public nextScheduledTime As Date

Sub CreateNewSchedule()
    nextScheduledTime  = DateAdd("n", 1, Now)
    Application.OnTime EarliestTime:=nextScheduledTime , Procedure:="macro_name", Schedule:=True
End Sub

Sub macro_name()

    If Value > 10 Then
        SendEmail
    Else
        CreateNewSchedule
    End If

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
    Application.OnTime EarliestTime:=nextScheduledTime, Procedure:="macro_name", Schedule:=False
End Sub

You can then continue to use Excel between the scheduled times.

1
  • Thank you. Your CreateNewSchedule function works perfectly. I didn't know about that feature Commented Jul 6, 2015 at 21:36
2

I think you need to specifically process the application event stack with a DoEvents call. This allows user interactions with the spreadsheet to occur, where normally the macro would take precedence. You code would look something like:

Do While True

    If Value > 10 Then
        SendEmail
    End If

    Sleep 60*CLng(1000)

    DoEvents
Loop

You could also construct a GUI with HTA if you wanted to remain with VBScript.

3
  • 2
    Another way would be to set a macro to run whenever the spreadsheet changes.
    – Holmes IV
    Commented Jul 3, 2015 at 16:37
  • Yes, that's a better solution to be honest, because you'll still get some application lock-up here as a result of the sleep command, I think. I suggest you post it as an answer to this question - I'd upvote it :-). The only problem would come if no user interaction occurred, but a mail needed to be sent. In that case, if the sleep command isn't actually timing something, you could just reduce it to 33ms or something un-noticable.
    – Orphid
    Commented Jul 3, 2015 at 16:38
  • @HolmesIV, see my response in the comments above. Commented Jul 3, 2015 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.