• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Application.OnTime Reopens Workbook After Save and Close|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Application.OnTime Reopens Workbook After Save and Close|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosApplication.OnTime Reopens Workbook…
sp_PrintTopic sp_TopicIcon
Application.OnTime Reopens Workbook After Save and Close
Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
1
June 22, 2020 - 6:59 pm
sp_Permalink sp_Print sp_EditHistory

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
 
 
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
June 23, 2020 - 11:19 am
sp_Permalink sp_Print

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

Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
3
June 23, 2020 - 12:28 pm
sp_Permalink sp_Print

Debug-worksheetVBA.jpgImage Enlarger

Debug.jpgImage Enlarger

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
sp_PlupAttachments Attachments
  • sp_PlupImage Debug-worksheetVBA.jpg (60 KB)
  • sp_PlupImage Debug.jpg (19 KB)
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
June 23, 2020 - 1:23 pm
sp_Permalink sp_Print

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

Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
5
June 23, 2020 - 2:00 pm
sp_Permalink sp_Print

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.jpgImage Enlarger

Debug-1.jpgImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage Debug-worksheetVBA-1.jpg (63 KB)
  • sp_PlupImage Debug-1.jpg (14 KB)
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
June 23, 2020 - 3:58 pm
sp_Permalink sp_Print

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

Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
7
June 23, 2020 - 5:54 pm
sp_Permalink sp_Print

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 

Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
8
June 24, 2020 - 8:37 am
sp_Permalink sp_Print

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
Avatar
Steven Behr
Member
Members

Power Query
Level 0
Forum Posts: 85
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
9
June 25, 2020 - 6:59 am
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
10
June 25, 2020 - 9:38 am
sp_Permalink sp_Print

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 

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
11
June 25, 2020 - 9:54 am
sp_Permalink sp_Print

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

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Catalin Bombea, Velouria
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 204
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Bhuwan Devkota
Kathryn Patton
Maria Conatser
Jefferson Granemann
Glen Coulthard
Nikki Fox
Rachele Dickie
Forum Stats:
Groups: 3
Forums: 24
Topics: 6222
Posts: 27291

 

Member Stats:
Guest Posters: 49
Members: 31913
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.