VBA application Ontime - vba

I got some problem with application.ontime maybe somebody knows the solution
From the beggining
Private Sub Workbook_Open()
'some code
call macro_name
end sub
sub macro_name
'some code refreshing data on form
call start_timer
end sub
dim start_time as date
Sub start_timer()
start_time = Now() + TimeValue("00:00:05")
Application.OnTime start_time, "macro_name"
End Sub
Sub end_timer()
Application.OnTime start_time, "macro_name", , False
'MsgBox "done" i put this to be sure that code is executed
End Sub
Private Sub Workbook_BeforeClose(cancel As Boolean)
'some code
call end_timer
end sub
problem is that the timer doesn't stop. If i have some other workbook open and i close the one with ontime code it starts again at scheduled time
if i put buttons on a form
Private Sub CommandButton1_Click()
start_timer
End Sub
Private Sub CommandButton2_Click()
end_timer
End Sub
everything works ok and ontime is canceled.
I solved it partly by putting the
call end_timer
on the form closing button
Private Sub Image1_Click()
end_timer
ThisWorkbook.Close
End Sub
but if workbook will be closed some other way than button ontime won't be canceled
sorry for my english i'll try to explain better if something is unclear

Related

Call public sub each day at specific time without reopening the file again

I have this public sub defined in Module 1 of my excel workbook
Public Sub AutoUpdate()
Application.OnTime TimeValue("22:40:00"), "GetTime"
End Sub
And in my workbook code i have this to call this AutoUpdate method.
Public Sub Workbook_Open()
Call AutoUpdate
End Sub
My problem is that when i open the file it ways the time to be 22:40:00 and it calls "GetTime" but then on the next day the excel either crashes or the function is not called again.
Is this Application.OnTime method working for each day without needing to reopen ( refresh ) the workbook again.
Try using the following code:
Private Sub Workbook_Open()
Call DailyRun
End Sub
DailyRun code:
Sub DailyRun()
Application.OnTime TimeValue("22:40:00"), "GetTime"
Application.OnTime TimeValue("23:59:00"), "DailyRun"
End Sub
The DailyRun macro calls itself at the end of each day to create new Ontime event.
If you want to run the macro using Windows Task Scheduler, add your macro to Workbook Open event:
Private Sub Workbook_Open()
Application.OnTime TimeValue("22:40:00"), "GetTime"
Me.Close
End Sub
Don't forget to close your workbook.

vba start stop timers?

I want to start and stop two timers in my excel worksheet. This is what i have:
When worksheet is opened, this module is triggered:
Sub SetOpenTimer()
OpenTimer = Now + TimeValue("00:00:10")
Application.OnTime OpenTimer, "StartTimerShutdownForm"
End Sub
After 10 seconds (in this example), the sub startimershutdownform is triggered:
Sub StartTimerShutdownForm()
UserForm1.Show
End Sub
This displays a userform where a message is displayed. In the initialize sub in the Userform a sub setformtimer is triggered:
Sub setFormTimer()
FormTimer = Now + TimeValue("00:00:10")
Application.OnTime FormTimer, "Shutdown"
End Sub
If this timer in setformtimer ends, the sub shutdown is triggerd
this sub closes the workbook, but in the Userform there is a button for the user to cancel the setformtimer:
Sub stopFormTimer()
Application.OnTime EarliestTime:=FormTimer, _
Procedure:="setOpenTimer", Schedule:=False
End Sub
This suppose to stop the setformtimer, but it does not work.
When this timer is stopped, it suppose to restart the timer that is triggered when the document is being opened, so it all restarts, but the stop timer code does not work.
In the module where stopFormTimer, setFormTimer and StartTimerShutdownForm is located, at the top I declare these
Dim OpenTimer As Date
Dim FormTimer As Date
What am i missing here? Why cant i stop the timer and restart the first timer?
I think the problem is you have different proc names in the two subs. They must match. I'd try:
Sub setFormTimer()
FormTimer = Now + TimeValue("00:00:10")
Application.OnTime EarliestTime:=FormTimer, Procedure:="Shutdown", Schedule:=True
End Sub
Sub stopFormTimer()
Application.OnTime EarliestTime:=FormTimer, Procedure:="Shutdown", Schedule:=False
End Sub

Trigger code after inactivity [duplicate]

This question already has answers here:
Automatically close Workbook after inactivity
(2 answers)
Closed 7 years ago.
I want my WB to trigger some code after inactivity time (set by me, naturally). I could only find code to close the WB after inactivity time, but I want the code to do something else, different from closing the WB. I found this code for closing the WB:
This Workbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
stop_Countdown
ThisWorkbook.Save
End Sub
Private Sub Workbook_Open()
start_Countdown
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
stop_Countdown
start_Countdown
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
stop_Countdown
start_Countdown
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
stop_Countdown
start_Countdown
End Sub
Regular module:
Option Explicit
Public Close_Time As Date
Sub start_Countdown()
Close_Time = Now() + TimeValue("00:00:10")
Application.OnTime Close_Time, "close_WB"
End Sub
Sub stop_Countdown()
Application.OnTime Close_Time, "close_WB", , False
End Sub
Sub close_wb()
ThisWorkbook.Close True
End Sub
In what part of the code should I introduce the events I want the WB to do, after inactivity, instead of closing the WB?
You need to make changes in the Regular module.
Instead of passing the String close_wb() in your Application.OnTime function call, you have to specify the name of the procedure containing whatever you want to perform.
Here is the code to get you started:
Option Explicit
Public Inactivity_Time As Date
Sub start_Countdown()
Inactivity_Time = Now() + TimeValue("00:00:10")
Application.OnTime Inactivity_Time, "my_procedure" ' <- Notice that I changed the name of the procedure here
End Sub
Sub stop_Countdown()
On Error Resume Next
Application.OnTime Inactivity_Time, "my_procedure", , False ' <- And here too.
On Error GoTo 0
End Sub
Sub my_procedure()
' The code to perform you event goes here
End Sub
Please check here for more details on the Application.OnTime method.
EDIT: After some test, it appears that you can't call stop_Countdown() in the Workbook_BeforeClose sub procedure: It throws an error. According to this post, in your Workbook module, you have to replace the procedure Workbook_BeforeClose with the following one:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Close_WB
End Sub
And add the following procedure:
Public Sub Close_WB()
stop_CountDown
ThisWorkbook.Close SaveChanges:=True
End Sub

Killing Application.ontime with Workbook_BeforeClose

I looked through every topic I've found in here, but couldn't figure what is wrong with the below.
I am using Application.OnTime to run a Module every minutes. The problem is, when I close the worksheet, Excel reopens the sheet the next time the Macro should run.
I have attempted to use the following fix, to no avail.
Option Explicit
Dim dTime As Date
Public Function AutoRun2()
dTime = Now + TimeValue("00:01:00")
Application.OnTime dTime, "AutoClear"
End Function
Sub StopAutoRun2()
Application.OnTime dTime, "AutoRun2", , False
End Sub
and I have the following in ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopAutoRun
Call StopAutoRun2
End Sub
What am I doing wrong here?
Change "AutoRun2" to "AutoClear" in your stop sub as this is the actual macro that you want to stop being scheduled with the False argument.

How to go back or re-display Userform after hiding it?

I have this code in one command button in UserForm2:
Private Sub CButton1_Click()
UserForm1.Show
Me.Hide
End Sub
Now, Userform1 is shown.
Then I have another code in one command button in Userform1:
Private Sub CButton2_Click()
UserForm2.Show
Unload Me
End Sub
This throws up a:
Runtime Error: Form already displayed; can't show modally
How do I do this properly?
How do I go back to the previous Userform after hiding or unloading it?
I think the problem is the order of the statements. I found out by using the debugger that when I had the Show statements before the Hide or Unload, these last are not executed.
Try this
' on UserForm2
Private Sub CommandButton1_Click()
Me.Hide
UserForm1.Show
End Sub
' on UserForm1
Private Sub CommandButton1_Click()
Me.Hide
UserForm2.Show
End Sub
Change to this:
Private Sub CButton1_Click()
Me.Hide
UserForm1.Show
Unload Me
End Sub
Private Sub CButton2_Click()
Me.Hide
UserForm2.Show
Unload Me
End Sub