Search code examples
excelvbatimer

VBA code to run on 10 minute intervals only during work hours


I want my code to run on 10 minute intervals throughout the work day, but stop when the work day is over. Currently my code uses

Application.OnTime Now + TimeValue("00:10:00"), "!Module1.Refresh3"

but it causes problems as it continues to run all night.


Solution

  • You could do something along the lines of:

    If Hour(Now) > 19 Then
        Application.Wait (Now + TimeValue("12:00:00"))
    End If
    

    This would stop your code from 8pm onwards for 12 hours. For clearification: As peakpeak mentioned, make sure you run this in your outer loop, so after every 10 minute interval these lines get evaluated.