We really just want to check if the send_mail sub is being called, right? You claim that the send_mail sub is not being called. To check this, we write the simplest code. If there is data in column J, it means that the send_mail sub is being called.
Public k As Long
...
Sub send_mail()
k = k + 1
Worksheets("Schedule").Range("J" & k).Value = Format(Now, "yyyy.mm.dd hh:mm")
UpdateClock
End Sub
I will provide the file again, but you must ensure:
1. The file contains only 1 sheet Schedule and my code, without your sheets and codes. Because we will only check my code, right? We do not know what your code does, and whether it does not affect my code. And we only want to check my code.
2. You cannot change, add, delete anything.
3. Download the attached file --> run the file --> leave it for a few hours --> see if there is data in column J entered by sub send_mail.
code in ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime lastTime, "send_mail", , False
End Sub
Private Sub Workbook_Open()
UpdateClock
End Sub
code in Module1
Option Explicit
Public lastTime As Double
Public k As Long
Private Function readTime()
Dim day As Long, lastRow As Long, i As Long, curr_time As Double
curr_time = 0
day = Weekday(Date)
With Worksheets("Schedule")
lastRow = .Cells(Rows.Count, day).End(xlUp).Row
For i = 2 To lastRow
If .Cells(i, day).Value > Time Then
curr_time = .Cells(i, day).Value
Exit For
End If
Next i
If curr_time = 0 Then curr_time = Date + 1 + .Cells(2, (day Mod 7) + 1).Value
End With
readTime = curr_time
End Function
Sub UpdateClock()
Dim curr_time As Double
curr_time = readTime
If curr_time = 0 Then
Application.OnTime lastTime, "send_mail", , False
Else
lastTime = curr_time
Application.OnTime lastTime, "send_mail"
End If
End Sub
Sub send_mail()
k = k + 1
Worksheets("Schedule").Range("J" & k).Value = Format(Now, "yyyy.mm.dd hh:mm")
UpdateClock
End Sub
I added in function readTime
If curr_time = 0 Then curr_time = Date + 1 + .Cells(2, (day Mod 7) + 1).Value
In the previous readTime the code reaches the last hour and turns off the clock. With the above line the code reaches the last hour then takes the first hour of the next day.
Bookmarks