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
 

Forum statistics

Threads
1,081,705
Messages
5,360,759
Members
400,595
Latest member
T_Dubs

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top