Forum

Application.OnTime ...
 
Notifications
Clear all

Application.OnTime Reopens Workbook After Save and Close

11 Posts
2 Users
0 Reactions
531 Views
(@stevenbehr1)
Posts: 92
Estimable Member
Topic starter
 

Hi,

I have taken an extract of my VBA timer

I have created a timer in my workbook but every time I hit my save and close button on my interface TAB it saves and close but opens the workbook again or loops it.

In VBA is as follows

Sub StartTimer()
Application.OnTime Now + TimeValue("00:00:01"), "Next_moment"
End Sub
Sub StopTimer()
Application.OnTime Now + TimeValue("00:00:01"), "Next_moment", , False
End Sub
Sub Next_Moment()
If Worksheets("Interface").Range("A2").Value = 0 Then Exit Sub
(DEBUGS) Worksheets("Interface").Range("A2").Value = Worksheets("Interface").Range("A2").Value - TimeValue("00:00:01")
StartTimer
End Sub
 
My usual close and save feature is
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
 
Can you please tell me why is debugging the Next_Moment() at worksheet("Interface")…..
 
I just want to save and close my workbook
 
Thanks
 
steve
 
 
 
Posted : 23/06/2020 4:59 am
Philip Treacy
(@philipt)
Posts: 1632
Member Admin
 

Hi Steve,

You need to call StopTimer before you close the workbook.

Application.OnTime is set at the application level i.e. the Excel program itself.  It's not set within the open workbook.

So when you close the workbook the timer is still running and Excel opens the workbook again to run the Next_Moment sub.

As for your debug error, I can't reproduce it.  What did the error message say?  Screenshot?  What value is in A2 when the error occurs?

Regards

Phil

 
Posted : 23/06/2020 9:19 pm
(@stevenbehr1)
Posts: 92
Estimable Member
Topic starter
 

Debug-worksheetVBA.jpgDebug.jpg

The Value In Sheet1.RangeA2  is just a custom format hh:mm:ss----its just a timer

But for some reason its highlighting VBA in SUb Next_Moment()

Worksheets("Interface")….

 

Indicating before debug Run-time error '1004'

Something about changing protected sheet on a cell or chart that changing during protection. The only protection I have got is the 12 TAB except for sheet1.

Perhaps I have got 2 problems.

1. Some protection issue????

2. its still looping soon as I hit Log out and save the code for this button is

Private Sub CommandButton3_Click()
On Error Resume Next

StopTimer
'save workbook before closing:
ThisWorkbook.Save

ThisWorkbook.Close
End sub
 
Perhaps if you have a solution for the looping back to open workbook and I look into what protection I may have forgotten.
 
Thanks
 
Steve
 
Posted : 23/06/2020 10:28 pm
Philip Treacy
(@philipt)
Posts: 1632
Member Admin
 

In the attached workbook I've added a call to StopTimer like this

Private Sub CommandButton1_Click()
StopTimer
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

and the workbook closes without reopening when I click on 'Log Out and Save'.

The error states you are trying to change a cell on a protected sheet.  So you must have that sheet protected from changes.

Regards

Phil

 
Posted : 23/06/2020 11:23 pm
(@stevenbehr1)
Posts: 92
Estimable Member
Topic starter
 

Hi Phil,

 

Thanks for that now its debugging method on Application.Ontime in stoptimer???.Please see attached JPG

Application.OnTime Now + TimeValue("00:00:01"), "Next_moment", , False

Should I put all the Timer codes in the Log out as a Private Sub???

I found the offending protection the one line in my VBA before closing a sub routine!

So I did this below, then when it opens workbook I put the interface on protection so that no one touches any data!

Unprotect_All
StopTimer
ActiveWorkbook.Save
ActiveWorkbook.Close

 

Debug-worksheetVBA-1.jpgDebug-1.jpg

 
Posted : 24/06/2020 12:00 am
Philip Treacy
(@philipt)
Posts: 1632
Member Admin
 

Hi Steve,

It's very difficult for me to say why it's erroring as I don't have the code or know what preceded the error.  But that error is because there is no OnTime call set for Next_moment.

Everything works fine for me in the file I posted so I'd try to replicate that at your end.  As you are working with different code I can't say why you're getting these errors.

Phil

 
Posted : 24/06/2020 1:58 am
(@stevenbehr1)
Posts: 92
Estimable Member
Topic starter
 

Thanks Phil,

 

Let me try tomorrow on my work computer 365 see if it works I have sneaky suspicion that on some occasions, some codes just don't work on 2019, but on 365 it has.

 

Also, how do I call Ontime in a code. Would be in workbook open or in the log out?.

Also every now then the code on your attached sent back also debugged with the same error.

 

Anyway, I'll try tomorrow on 365

 

Steve 

 
Posted : 24/06/2020 3:54 am
(@stevenbehr1)
Posts: 92
Estimable Member
Topic starter
 

Hi Phil,

I tried 365 and there still a debug.

However, I have an alternative. I have made an internal timer and it works well. So long as the prescribed time limit to having a spreadsheet open, it will save and close if a person leaves there desk for a long period of time.

But now, Wondering in your infinite wisdom, can I still have a visible timer from the starttimer code above within this code below.

I hand you my Excel file!

Sub Time()
Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
'StartTimer
Application.DisplayAlerts = True
TimeInMinutes = Sheet2.Range("A2").Value 'Timer is set for 180 minutes; change as needed.
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
   Start = Timer
   Do While Timer < Start + TotalTimeInMinutes
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
  Application.DisplayAlerts = False
   MsgBox "This file has been open for " & TotalTime / 60 & " minutes.  You have 5 minutes to save before Excel closes."
End If
Start = Timer
   Do While Timer < Start + (5 * 60)
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
  'Application.DisplayAlerts = False
  'MsgBox "Excel will now close."
  
   ActiveWorkbook.Save
   Application.DisplayAlerts = True
   ActiveWorkbook.Close
 
Thanks
 
Steve
 
Posted : 24/06/2020 6:37 pm
(@stevenbehr1)
Posts: 92
Estimable Member
Topic starter
 

Hi Phil.

 

I found the reason why the code below is debugging.

Application.OnTime Now + TimeValue("00:00:01"), "Nexttick", , False

When I copy the code below into Private Sub Workbook_Open()

Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"

ActiveWindow.DisplayWorkbookTabs = False
Application.DisplayStatusBar = False
Application.DisplayFormulaBar = False
' Making the Ribbon visible is false
Application.DisplayFullScreen = False
ActiveWindow.DisplayHeadings = False

ActiveWindow.DisplayWorkbookTabs = Not ActiveWindow.DisplayWorkbookTabs

Application.ScreenUpdating = True

 

It would be the Application code that somehow needs to be implemented into the mainstream workbook open.

After trying doing various codes that don't work. Is there a way to call this application.OnTime in open()

Thanks

Steve

 
Posted : 25/06/2020 4:59 pm
Philip Treacy
(@philipt)
Posts: 1632
Member Admin
 

Hi Steve,

You said you found the reason for the debug but it's not clear what that was.

You already are calling Starttimer in Workbook_Open and the timer is running when I open the workbook you attached.

It would be the Application code that somehow needs to be implemented into the mainstream workbook open.

Haven't you already done that?  You said so at the start of your post.

When I copy the code below into Private Sub Workbook_Open()

Regards

Phil 

 
Posted : 25/06/2020 7:38 pm
Philip Treacy
(@philipt)
Posts: 1632
Member Admin
 

Hi Steve,

When the timer reaches zero, the value in B1 is zero.  If you close the workbook in that condition, you are calling Stoptimer from Workbook_BeforeClose, but Stoptimer is trying to run this

Application.OnTime Now + TimeValue("00:00:01"), "Nexttick", , False

which causes a debug because there is no Nexttick scheduled. 

Check if B1 is 0 before calling Application.OnTime Now + TimeValue("00:00:01"), "Nexttick", , False

Regards

Phil

 
Posted : 25/06/2020 7:54 pm
Share: