Ontime Event Going haywire at midnight

Drumin_Phreak

New Member
Joined
Oct 10, 2016
Messages
25
Hello! I have a excel project that starts an ontime event on workbook open. This thing works fantastic! However, when midnight rolls around it crashes. After doing some research I have surmised that the problem is when the date changes, the time is now After the initial time set. I tried to include the now function (dTime2= Now + TimeValue ("00:05:00") and this does nothing. The one that currently works is:

Code:
dTime2 = Time + TimeValue("00:05:00")
  Application.OnTime dTime2, "AutoSaves"
I have been struggling with this for quite sometime. If I could get any help it would greatly be appreciated.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
When do you want 'AutoSaves' to run?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,351
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Use Now rather than Time, since that will include a Day part.
 

Drumin_Phreak

New Member
Joined
Oct 10, 2016
Messages
25
Open Resumption of Midnight shift, I did indeed try the "NOW" function so my code is

Code:
[COLOR=#333333]dTime2 = Now + TimeValue("00:05:00")
[/COLOR][COLOR=#333333]  Application.OnTime dTime2, "AutoSaves"[/COLOR]
However, when the hour struck Midnight it froze up and went into an infinite loop trying to save.

My next idea is:

Can I some how put a timer when the workbook is opened to suspend auto saving at like 23:45 and Resume saving at 00:05 the next day?
 

Drumin_Phreak

New Member
Joined
Oct 10, 2016
Messages
25
So it seems like no matter what I do it still goes crazy right at midnight, they do a reload and it works perfect from there on out.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,351
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
I can't see why it would do what you're describing but as a workaround, you could do something like this:

Code:
dTime2 = Now + TimeValue("00:05:00")
if Hour(dTime2) = 23 and Minute(dTime2) >=50 then dTime2 = Date() + 1 + TimeValue("00:05:00")
 

Drumin_Phreak

New Member
Joined
Oct 10, 2016
Messages
25
I can't see why it would do what you're describing but as a workaround, you could do something like this:

Code:
dTime2 = Now + TimeValue("00:05:00")
if Hour(dTime2) = 23 and Minute(dTime2) >=50 then dTime2 = Date() + 1 + TimeValue("00:05:00")

RoryA,

I sincerely appreciate your suggestions, but nothing I do seems to help this situation. I am so confused about what i can do to fix this issue.
 

Forum statistics

Threads
1,082,342
Messages
5,364,783
Members
400,815
Latest member
gangstar67

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top