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