Application.OnTime Unreliable

Nkolanko

New Member
Joined
Jun 22, 2017
Messages
2
Hey all,

So I'm coding a program which I want several macros to run about every hour of the day. With my current implementation it works rather often but on a few occasions I'll come into work and notice that new day has not triggered. This happens about once a week without pattern (fails on different days). If anyone has some better, more reliable implementation please let me know.

Note, so far we've had the PC which runs this code off on the weekends anyways.



Private Sub Workbook_Open()
'If it is not the weekend
If (Weekday(Date) <> 1 Or Weekday(Date) <> 7) Then

'If it is a new day
If (Range("B1").Value <> WeekdayName(Weekday(Date))) Then
Call SaveYesterday
End If

'Continue looping until correct day is found
Do While (Range("B1").Value <> WeekdayName(Weekday(Date)))
Call NewDayV02
Loop

Application.OnTime TimeValue("5:55:00"), "SaveYesterday"
Application.OnTime TimeValue("6:00:00"), "NewDayV02"
Application.OnTime TimeValue("7:00:00"), "SaveToday"
Application.OnTime TimeValue("8:00:00"), "SaveToday"
Application.OnTime TimeValue("9:00:00"), "SaveToday"
Application.OnTime TimeValue("10:00:00"), "SaveToday"
Application.OnTime TimeValue("11:00:00"), "SaveToday"
Application.OnTime TimeValue("12:00:00"), "SaveToday"
Application.OnTime TimeValue("13:00:00"), "SaveToday"
Application.OnTime TimeValue("14:00:00"), "SaveToday"
Application.OnTime TimeValue("15:00:00"), "SaveToday"
Application.OnTime TimeValue("16:00:00"), "SaveToday"
Application.OnTime TimeValue("17:00:00"), "SaveToday"
Application.OnTime TimeValue("18:00:00"), "SaveToday"
Application.OnTime TimeValue("19:00:00"), "SaveToday"
Application.OnTime TimeValue("20:00:00"), "SaveToday"
Application.OnTime TimeValue("21:00:00"), "SaveToday"
Application.OnTime TimeValue("22:00:00"), "SaveToday"
Application.OnTime TimeValue("23:00:00"), "SaveToday"
Application.OnTime TimeValue("23:59:00"), "SaveToday"
End If
End Sub

'MACROS

Sub NewDayV02()
Application.OnTime Now + 1, "NewDayV02"
If (Weekday(Date) = 1 Or Weekday(Date) = 7) Then
Exit Sub
End If
Application.ScreenUpdating = False
Range("B2:D62").Select
Selection.ClearContents
Columns("B:D").Select
'
' Lots of code
'



End Sub



Sub SaveToday()
Application.OnTime Now + 1, "SaveToday"
If (Weekday(Date) = 1 Or Weekday(Date) = 7) Then
Exit Sub
End If
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub



Sub SaveYesterday()
Application.OnTime Now + 1, "SaveYesterday"
If (Weekday(Date) = 1 Or Weekday(Date) = 7) Then
Exit Sub
End If
Dim currWorkboox As Excel.Workbook
Set currWorkbook = ActiveWorkbook
Range("A1", "Q30").Copy
Application.DisplayAlerts = False
Workbooks.Open (ThisWorkbook.Path & "\YesterdayFloorDisplayV02.xlsx")
Range("A1").Select
ActiveSheet.Paste

Application.DisplayAlerts = False
ActiveWorkbook.Save
ActiveWorkbook.Close

currWorkbook.Activate
ActiveWorkbook.Save

Application.DisplayAlerts = True
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top