OnTime Method problem near midnight

Geoff Lambert

New Member
Joined
Sep 25, 2011
Messages
7
I have an Excel macro to download a web-based Excel sheet . I run this macro once per hour, over a 24 hour period, using the OnTime Method, thus

Start macro
Inputbox to Call for original start time,
Inputbox to Call for # of times to run (N)

The Macro runs N times.... at the end of each run of the macro, the OnTime method is called again with the new start time = original start time + N * 1 hour.

This works (and I have been using it for weeks), except under one condition... when the new start time is exactly midnight or "close to" midnight (plus or minus approx a minute, I think). In this case, the macro runs TWICE, the second instance about 15 seconds after the first. For reasons associated with the downloaded data being identical, this then causes the macro to bomb out.

I know that the macro has selected its new Start time correctly one hour hence (i.e. approx 1 AM), because I drop that value into a cell on my own spreadsheet for double-check purposes. Thus it seems to me that another OnTime Instance out of my control, but with my macro as its object, has been run.

I gather that the OnTime Method uses the VBA timer() function and that the VBA timer is reset at midnight each day.

This would be the explanation except for the fact that the macro will repeat happily all through the day and night if the original start time is (say) xx:30:00. The problem only occurs when the original start time is near the "top of the hour" at (say) xx:59:40 and then only at the call at time 23:59:40 and not at any other "top of the hour"

How can I fix this?
 
Uh
Old versions of this macro used the following function to calculate NextTime
NextTime = Now + TimeIncr
but this introduced timing jitter caused by variable time lengths that the macro took to run. This macro always runs all night without a hitch, but it is always set to the "bottom of the hour" and so never runs at midnight.

You could recalc NextTime = Now + TimeIncr at the top of the "Hourly_Train_Catcher" macro. That should eliminate or minimize the jitters.

I don't have any new insight on the other "stuff".
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Problem solved

It was to do with the fact that "day1 23:00:00" + "01:00:00" produces "day1 00:00:00", not "Day2 00:00:00", so the macro fires off a second time ajust s you surmised. Changing the start time to be 1 second past the hour fixes this. This is an advantage in another way too because the database I download itself takes a fraction of a second to tick over at midnight (at precisely midnight it is empty). The macro with NextTime = Now + TimeIncr placed at the head of the macro also works, but still with a bit of creep.

Now to see what happens when DST starts here on Saturday night

Thanks for guiding me on this.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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