
Enter the hours of work and relevant employee positions under each day of work. The vertical X-axis is for your employees. Step 1: Enter the Employee Names and Dates.Make sure that the name of the procedure exactly same as you have provided in the Application.OnTime method in above code. Last but not the least step – write your own procedure which you want to schedule it. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Īpplication.OnTime RunWhen, "SaveAndCloseWorkBook",, False 'Now re-schedule the Save&Close function with the new calculated time. ' Now reset the runTime variable again based on the new time ' As explained above to reset the scheduled time for this save and close procedureĪpplication.OnTime runTime, "SaveAndCloseWorkBook",, False Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) ' Now schedule the Save&CloseWorkbook procedure with the specified durationĪpplication.OnTime runTime, "SaveAndCloseWorkBook",, True RunTime = Now + TimeSerial(DURATION_IN_HOURS, DURATION_IN_MINUTES, DURATION_IN_SECONDS)

' To change the time you can change in the public Constants ' Set the runTime public varibale with the specified duration
VBA APPLICATION ONTIME CODE
Step 2#Ĭopy and paste the following code in your ThisWorkbookCode Window. In this function I have used it for 30 minutes. You can use this duration according to your need. It should not be pasted after any function/procedure in the module. Note that the following 2 lines must be added at the top of the module.
VBA APPLICATION ONTIME HOW TO
Read basic tutorials – how to add modules in VBE. It will automatically save and close the workbook if it was left idle for certain time (you can configure whatever you wish to)Ĭopy and paste the following code in any module in your VBE Project. In such case, this little macro will help. Incase any user left the excel open after working then, it will not be availbale for other users until he himself logs in and close the excel. This is very useful when a workbook is shared to be used by more than one user.

If any of the above events are happening with workbook, idle duration will be reset back to zero again. Here I am checking following two events to determine if excel was left idle. Last but not the least step, copy paste the Save and Close workbook function which will be triggered at scheduled time in any of the regular module in your VBE project.Įxample 2# Save and Close workbook, if left idleīefore we proceed further, let see – How to check if excel was left idle? 'Before closing the workbook, remove the scheduleĪpplication.OnTime VBA.TimeValue("17:30:00"), "SaveAndCloseWorkBook",, False Private Sub Workbook_BeforeClose(Cancel As Boolean) ' Schedule the Save&CloseWorkbook procedure at 5:30 PMĪpplication.OnTime VBA.TimeValue("17:30:00"), "SaveAndCloseWorkBook",, True

Example 1# Schedule to save and close workbookįollow the below steps to achieve this: Step 1#Ĭopy and paste the following code in your ThisWorkbook Code window False is used to clear out previously scheduled procedure. Schedule : This is an optional parameter. Since this is an optional parameter, if omitted, Application.OnTime method will wait till excel does not finish all its activities. Note that, if other ongoing activities are not complete within the specified waiting time then the procedure will not run. This is a maximum waiting time set by programmer for Excel to complete any ongoing activity like Cut, Copy, Paste, Save etc. LatestTime : This is an optional parameter. It is the name of the procedure/macro you want to run. Procedure: This is also a mandatory parameter.

Lets have a look at the syntax of this Excel VBA method: Syntax:Īpplication.OnTime ( EarliestTime, Procedure, LatestTime, Schedule ) Where:ĮarliestTime: This is a mandatory parameter and this is the time when you want to run your procedure. This method is used to execute a procedure in Excel VBA at a specific time or after certain time has elapsed. How to Schedule a workbook to close if left idle for specified duration How to schedule a workbook to close at specified time
