OnTime runs at the wrong time

CDienst

New Member
Joined
Jul 11, 2018
Messages
31
Hello,

I've got one workbook with 2 macros (I did not originally create them): macro1 resets some cell values (for graphical purposes only) and then sets a string to equal a time (i.e. 8:30AM) in a cell and then uses OnTime with that string to schedule macro 2 for that time. Macro2 then makes a pdf from the sheet and emails it out. This report worked fine, originally.

The other workbook has different data, but the setup with regards to the macros is similar. I has the same 2 macros from the first workbook. I copied them over and renamed the macros, named ranges and references to such to make sure they didn't intersect (even though I don't think they should have). This second report only runs once a week, on the day specified by a dropdown on the sheet. In this case, macro1's OnTime schedules macro3 which checks the day of the week. If it is wrong day, it runs macro1 so it will check again tomorrow. If it is the correct day, it runs macro 2.

Since these reports are supposed to run automatically, I've added lines to the end of both macro2s so that they run their macro1s to run/check the next day. Everything comes out fine with regards to the reports and emailing, with the exception of the timing.

Sometimes the reports don't run at all and sometimes they run at the wrong time. Ideally the daily report runs every day at 8:30AM and the weekly report runs wednesday at 10PM. Unfortunately, I've had the weekly report get run at 8:30AM and the daily report run at 12:30PM.

I'm very confused as to why these sort of things keep happening. I really can't understand how they haven't implemented some sort of way to look at scheduled events. Is there some sort of better alternative to OnTime?

I don't know if it's relevant, but there's a DoEvents after the OnTime in macro1
Code:
'Start the clock
    Dim RptTime As String
    RptTime = Format(Range("RunTimeW"), "hh:mm:ss")
    Range("StatusW") = "Waiting since " & Now
    
    'Wait until start time, then go to week day test
    Application.OnTime TimeValue(RptTime), "WeekDayTest"
    If Not Application.CalculationState = xlDone Then
        DoEvents
    End If
End Sub
What purpose would the DoEvents serve? Could it be a problem?

I can post the rest of the code if anyone wants, but it's kinda long so I figured I'd wait until then.
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,224,930
Messages
6,181,829
Members
453,067
Latest member
mdiz777

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