Stop macro from repeating when using Application.OnTime - excel

By using the below code RefreshData I run mg macro every 10 secs.
I'm unable to stop stoprefresh which is assigned to a square shape.
Sub RefreshData()
Application.OnTime Now + TimeValue("00:00:10"), "mg", , True
End Sub
Sub stoprefresh()
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:10"), "mg", , False
End Sub
Sub mg()
ActiveSheet.Cells(ActiveSheet.Cells.Rows.Count, 1).End(xlUp).Offset(1) = "Running"
Call RefreshData
End Sub

Try this code
Option Explicit
Dim iTimerSet As Double
Sub RefreshData()
iTimerSet = Now + TimeValue("00:00:10")
Application.OnTime iTimerSet, "mg", , True
End Sub
Sub stoprefresh()
'On Error Resume Next
Application.OnTime iTimerSet, "mg", , False
End Sub
Sub mg()
ActiveSheet.Cells(ActiveSheet.Cells.Rows.Count, 1).End(xlUp).Offset(1) = "Running"
Call RefreshData
End Sub
Cancelling a scheduled Procedure
It is possible to cancel a procedure that has been scheduled to run but you need to know the
exact date and time it was scheduled for. To cancel a scheduled
procedure you must know the "EarliestTime" it was scheduled for.
Exactly the same syntax except you set the schedule paramater to
False. This tells the application to cancel the schedule.

Related

stop refreshing all queries at once in excel using macro

[![enter image description here][1]][1]I am refreshing all queries in the workbook after some specific interval using
this macro.
Sub macro_t()
ThisWorkbook.RefreshAll
interval = Now + TimeValue("00:00:015")
Application.OnTime interval, "macro_t"
End Sub
I want macro which will stop refreshing queries.
something like
stoprefreshall
how can I do it?
edit
the way i solve the issue is, stopping refresh of each sheet one by one
and then again run macro.
I want to stop refreshing all the sheets in single step, using macro
[
What you can do is declare a boolean type variable outside of procedure. And create a macro which will set that boolean variable to false and then macro will not run after run the `StopMacro(). Try below codes.
Public Times As Boolean
Sub start()
Range("E3").Value = Format(Now(), "HH:MM:SS")
Times = True
run
End Sub
Sub run()
If Not Times Then Exit Sub
Application.OnTime Now() + TimeValue("00:00:01"), "Run"
Range("E3").Value = Range("E3").Value + TimeValue("00:00:01")
End Sub
Sub StopMacro()
Times = False
End Sub
For better visualization see the screenshot.
Lots of trial and error
and finally this worked.
Sub Macro1()
'
' Macro1 Macro
'
'
With Sheets("r1").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("r2").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("r3").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("s1").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("s2").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("s3").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
End Sub

Workbook, which contains a clock, automatically reopens

I've got three Subs in Module: runClock, startClock and stopClock.
runClock is showing a clock that is updated every 5 seconds.
startClock is used to start the clock and is called from Sub Workbook_Open.
stopClock - is used to stop the clock and is called from Sub Workbook_BeforeClose.
However, when ever I close the workbook, it reopens automatically and the clock continues to run. I've checked the value of ClockOn and it's set to FALSE after the Workbook_BeforeClose is executed. I think that that issue has to do with Application.OnTime, but don't know how to fix it.
This Code is in Module:
Global ClockOn As Boolean
Sub runClock()
With Range("A1")
.Value = Now()
.NumberFormat = "hh:mm:ss"
If ClockOn = True Then
Application.OnTime Now + TimeSerial(0, 0, 5), "runClock"
End If
End With
End Sub
Sub startClock()
ClockOn = True
runClock
End Sub
Sub stopClock()
ClockOn = False
End Sub
This Code is in ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
stopClock
ActiveWorkbook.Save
End Sub
Private Sub Workbook_Open()
startClock
End Sub
This seems to be happening because the times resulting from Application.OnTime are not the same. The initial call time should be saved as a variable and then referenced in the subs. See this solution.
https://www.ozgrid.com/forum/forum/help-forums/excel-general/131658-timer-reopens-workbook-when-closed
Found a solution in http://www.cpearson.com/excel/OnTime.aspx
I've changed my sub stopClock so now it includes the following code:
RunWhen = Now + timeserial(0, 0, 5)
Application.OnTime earliestTime:=RunWhen, Procedure:="startClock", Schedule:=False
This stops the clock from running and the workbook from reopening.
I don't know what caused it to reopen under the previous code, but at least now it's fine.

Clearing Excel AutoFilter using VBA

I am using this VBA code to refresh my entire workbook at specific time intervals. (thanks to this thread)
As you can see, it is currently set to refresh every 60 minutes.
Public RunWhen As Double
Public Const cRunIntervalMinutes = 60
Public Const cRunWhat = "Workbook_RefreshAll"
Sub StartTimer()
RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, _
procedure:=cRunWhat, schedule:=False
End Sub
Sub Workbook_RefreshAll()
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
Call StartTimer
End Sub
The code works great, however there’s one thing I wish it could also do:
Any time the sheet is opened, and/or
Any time this VBA code executes the sheet refresh process
I would like the AutoFilter to be "Cleared" (not disabled or turned off) as in the screen grab below:
AutoFilter Clear
Thanks in advance for any assistance.
Cheers
This should do the trick. You may of course do it for one Sheet only, if you wish.
Sub Workbook_RefreshAll()
Dim sh As Worksheet
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
For Each sh In ActiveWorkbook.Worksheets
If Not (sh.AutoFilter Is Nothing) Then sh.AutoFilter.ShowAllData
Next sh
Call StartTimer
End Sub

Excel VBA Stopwatch: add pause button?

I have a stopwatch with a start/stop/clear button that posts the time to other cells in my spreadsheet. I want to add a pause button to allow users to pause and resume the timer where they left off, but it kept resuming and adding the time passed in the interim to the total. For example, I hit start and wait 5 minutes, then click pause at 5:00. I wait 2 more minutes, then click resume, and the timer picks back up at 7:00, 7:01, 7:02, etc. How do I create an effective pause/resume button and tell excel not to include the time passed when it resumes counting?
Dim NextTick As Date, t As Date
Sub StartStopWatch()
t = Time
Call StartTimer
End Sub
Sub StartTimer()
NextTick = Time + TimeValue("00:00:01")
Range("M1").Value = Format(NextTick - t - TimeValue("00:00:01"), "hh:mm:ss")
Application.OnTime NextTick, "StartTimer"
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=False
End Sub
Sub ResetTimer()
Range("M1").ClearContents
Range("N1").ClearContents
Range("L2").ClearContents
End Sub
Sub PauseButton()
If Range("L2").Value = "" Then
Range("L2").Value = "Paused"
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=False
Else
Range("L2").ClearContents
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=True
End If
End Sub
I think like this
Public blResume As Boolean
Dim NextTick As Date, t As Date
Sub StartStopWatch()
t = Time
Call StartTimer
End Sub
Sub StartTimer()
NextTick = Time + TimeValue("00:00:01")
Range("M1").Value = Format(NextTick - t - TimeValue("00:00:01"), "hh:mm:ss")
If blResume = False Then Exit Sub
Application.OnTime NextTick, "StartTimer"
End Sub
Sub Pause()
blResume = False
End Sub
Sub myResume()
blResume = True
StartTimer
End Sub

create a countdown timer in excel 2010 using VBA (excel)

I'm using Excel 2010, and want to create a countdown timer in excel. The code will be started and stopped by using buttons with macros attached to them. The problem occurs when the "start" button is pushed. The code above is utilizing Cell "B1" as the input spot, because I was just trying to get it to work, but every time I tried, it would always say:
"Cannot run the macro . The macro may not be available in this workbook or all macros may be disabled".
Yes I enabled all macros before putting this here. I want to be able to take user input, and to make that the time that the timer starts at instead of just using cell "B1".
'Macro for Starting the timer (attached to the start button)
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:01"), "nextTick"
End Sub
'Macro for next second
Sub nextTick()
Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")
startTimer
End Sub
'Macro for stopping the timer (attached to the end button)
Sub stopTimer()
Application.OnTime Now - TimeValue("00:00:01"), "nextTick", , False
End Sub
I modified your code very slightly and put it in a standard module:
Public Future As Double
Sub StartTimer()
Future = Now + TimeSerial(0, 0, 1)
Application.OnTime earliestTime:=Future, procedure:="nextTime", _
schedule:=True
End Sub
Sub nextTime()
Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")
StartTimer
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliestTime:=Future, _
procedure:="nextTime", schedule:=False
End Sub
Both StartTimer() and StopTimer() run just fine.

Resources