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
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