
Power Query

January 30, 2020

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 StopTimer()


October 5, 2010

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

Power Query

January 30, 2020



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


October 5, 2010

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

Power Query

January 30, 2020

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




October 5, 2010

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

Power Query

January 30, 2020

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

Power Query

January 30, 2020

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

Power Query

January 30, 2020

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


October 5, 2010

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


October 5, 2010

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
1 Guest(s)
