I am using Excel (Office 365).
here is a video with my problem: link
The internet is full of examples where people say that the code should work when we set Schedule to False but sadly that did not help me.
The code should start a cycle using Application.OnTime and stop it before closing the workbook.
If I run the function stopF manually, in the VBA Editor, it stops Application.OnTime properly.
But if I close the workbook, it still opens up and continues executing Application.OnTime.
' In Module1:
Public startingTime As Double
Sub startF()
MsgBox "hello"
startingTime = Now + TimeValue("00:00:10")
Application.OnTime startingTime, "Module1.startF"
End Sub
Sub stopF() 'HERE IT WORKS GOOD
On Error Resume Next
Application.OnTime startingTime, "Module1.startF", , False
End Sub
' In ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call stopF 'HERE IT DOES NOT WORK GOOD
End Sub
I don't have a clue what could I do more.
My temporary solution to my question:
I left the whole code like it was and added into Module1 following code:
' In Module1:
Sub closeWorkbook()
Call stopF
ThisWorkbook.Close
End Sub
The point is:
for manual closing stopF called from Workbook_BeforeClose will work
for vba closing stopF called from a module will work.
A bit more detailed:
when a user closes a workbook manually (closes the Excel window) then Workbook_BeforeClose => Call stopF will be triggered and it will stop Application.OnTime properly.
if I want to close the workbook via code then I do not rely on the Workbook_BeforeClose function (because it doesn't work for this somehow) but I call my new closeWorkbook() function when needed, which does terminate Application.OnTime and then closes the workbook.
Related
I am trying to get excel to run an update every XX seconds.
Sub StartTesting()
Call RunAll_ISP 'this call works
End Sub
Sub macro_timer() 'This sub schedules next run in XX seconds.
Dim ScanInterval
ScanInterval = Format(ThisWorkbook.Sheets("Configuration").Range("B5").Value, "hh:mm:ss")
Application.OnTime Now + TimeValue(ScanInterval), "RunAll_ISP", Schedule:=True ' !this macro call throws error!
End Sub
If I run Start_testing macro, call works.
If I run macro_timer macro, it waits desired number of seconds, and then throwsthis error:
Cannot run the macro "C:\location_of_file\filename.xlsm'!RunAll_ISP'. The macro may not be available in this workbook or all macros may be disabled
The settings for macros in the Trust center are set to "Enable VBA macros"
What am I doing wrong?
Your problem cannot be reproduced. This simple Example works.
So since your procedure StartTesting worked that means macros are definitely enabled, so the only option left is that your macro RunAll_ISP does not exist (or wrongly named).
Option Explicit
Public Sub RunAll_ISP()
MsgBox "I run"
End Sub
Public Sub CreateTimer()
Application.OnTime Now + TimeValue("00:00:05"), "RunAll_ISP", Schedule:=True
End Sub
I implemented some code into a few of my excel sheets that would cause the file to autosave periodically. The issue I am running into with the code, is that when it is executed, it reopens the files that have been closed that also contain the same code.
I am looking for a way to have VBA autosave documents every so often, but it would no longer run if the file isn't open.
This is the code I have implemented:
Contained in "ThisWorkbook":
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("03:00:00"), "Save1"
End Sub
Contained in "Module 3":
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("03:00:00"), "Save1"
End Sub
A note: The code between all of the documents is 100% identical (except the TimeValue, which varies by a few hours among some of them).
Try the next approach, please:
Adapt the OnTime call to a better qualified Sub:
Private Sub Workbook_Open()
scheduleTime = Now + TimeValue("03:00:00")
Application.OnTime scheduleTime, "Module3.Save1"
End Sub
Make the Sub in discussion Private, and create a new Public variable on top of the module:
Public scheduleTime As Date
Private Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
scheduleTime = Now + TimeValue("03:00:00")
Application.OnTime scheduleTime, "Module3.Save1"
End Sub
Clear the already set OnTime procedure:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime scheduleTime , "Module3.Save1", , False
End Sub
The first two items will solve the opening of other workbooks when the active workbook procedure is called. But, this should not happen and I am afraid that this is not the real opening mechanism, the workbooks are open by their own OnTime function call.
If my above supposition is True, the item 3 alone will solve the issue...
As you may notice, OnTime is an Application method, not a Workbook method.
This means that, even if you close the Workbook, so long as the Excel Application is still open that OnTime is still counting down. Once it hits zero, it will try to run the Subroutine - but the workbook is closed. Rather than throw an Error, Excel will reopen the Workbook, and then run the Subroutine.
In order to stop this, you will need to unscheduled the Subroutine before you close the Workbook, which will also mean Storing the time. So, you will need a Module that looks something like this:
Option Private Module
Option Explicit
Private AutoRunTime AS Date
Public Sub AutoSaveWorkbook()
ThisWorkbook.Save
AutoRunTime = Now()+TimeSerial(3,0,0) '3 hours
Application.OnTime AutoRunTime, "AutoSaveWorkbook", True
End Sub
Public Sub CancelAutoSave()
Application.OnTime AutoRunTime, "AutoSaveWorkbook", False
End Sub
Then, in the Workbook_Open even you call AutoSaveWorkbook to start things off, and in the Workbook_BeforeClose even you call CancelAutoSave to end them
I'm trying to close my workbook after a certain amount of time.
I'm using 10 seconds for now just to test it but it's not working automatically.
I have to run the code once by myself.
here's my code in module.
Public Sub fermeoutil()
Workbooks("OUTIL_CRN.xlsm").Save
Workbooks("OUTIL_CRN.xlsm").Close
Call test
End Sub
Sub test()
Application.OnTime Now + TimeValue("00:00:10"), "fermeoutil"
End Sub
In ThisWorkbook add procedure Workbook_Open with following code:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:10"), "fermeoutil"
End Sub
In Module1 keep your current procedure fermeoutil() removing the call to test:
Public Sub fermeoutil()
Workbooks("OUTIL_CRN.xlsm").Save
Workbooks("OUTIL_CRN.xlsm").Close
End Sub
Your call to test (or Workbook_Open() as it is now called), is not needed, as you have left out the last argument of Application.OnTime, namely Schedule which is optional and has a default value of True meaning the event will be recurring. Not sure if that really is your meaning, since you are closing the wb.
Are there some macros which can do it?
Any help would be appreciated - I am novice with VBA.
Please put this code in ThisWorkbook module. you can access this module by pressing double click on ThisWorkbook module inside VBA project.
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
then put this code in standard module. To insert standard module, right click on VBA project==>Insert==>Module. then paste code in newly created module.
Sub Save1()
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:01:00"), "Save1"
End Sub
Now when you open your workbook, it will automatically save every minute. You need to reopen Workbook in order to trigger Workboook_Open event.
I need to refresh the data from ole db source while opens excel and afte that saves and exit.
Here is my macro code:
Sub auto_open()
Call DataRefresh
End Sub
Sub DataRefresh()
TimeToRun = Now
Application.OnTime TimeToRun, "Refresh"
End Sub
Sub Refresh()
ActiveWorkbook.Connections("Shas").Refresh
End Sub
Sub auto_close()
Application.OnTime TimeToRun, "Refresh", , True
Application.Quit
ThisWoorkbook.Close SaveChanges:=True
End Sub
It's okay with renewing after openening but it doesnt exit. What am I doing wrong?
You don't actually tell the workbook to close at any point...
Also, the Auto_Open() routine is technically triggered after the workbook has finished opening so you don't need to call separate routines:
Also Also - You can't close a workbook after you've quit the application...
Try just this instead:
Sub auto_open()
ActiveWorkbook.Connections("Shas").Refresh
DoEvents
ThisWoorkbook.Close SaveChanges:=True
End Sub