Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timed Alarm in Excel VBA

I made a calendar to track tasks and similar items in Excel 2003. What I need to be able to do is to set a timer via VBA. Something like this:

run_in_x_secs ( timetowait, function to exec)

Is there a way to do that in excel vba, or should I try and find an alarm to run via the command line and run that from VBA.

How would you do it?

like image 352
Diego Castro Avatar asked Feb 26 '23 05:02

Diego Castro


1 Answers

This should do the trick:

Public Sub RunSubInXSeconds(secondsToWait As Long, nameOfFunction As String)
    Application.OnTime Now + secondsToWait/(24# * 60# * 60#), nameOfFunction 
End Sub

Although you do have to be a little careful with OnTime, especially if you're going to be setting it to wait for a long period of time... E.g. if you close the workbook before the timer expires and the Sub executes, then you can end up with your workbook opening itself to run it. Confusing if you don't expect it!

like image 73
Simon Cowen Avatar answered Mar 03 '23 08:03

Simon Cowen