VBA application.ontime is not working - excel

I am trying to achieve recursive functionality in macro using a code similar to this -:
Dim showTime As Boolean
Sub RunClock()
Range("A1", "A1").Value = Now
If showTime = True Then
Application.OnTime Now + TimeValue("00:00:01"), "RunClock"
End If
End Sub
Sub StartClock()
showTime = True
RunClock
End Sub
Sub StopClock()
showTime = False
End Sub
When I execute StartClock it shows time once then gives error - "Cannot run the macro 'Book1.RunClock'. The macro may not be available in this workbook or all macros may be disabled.

Try to prefix sheet name along with macro name. Excel might me be looking in whole work book with name like Sheet1.RunClock, so try as below (assuming the macro is in Sheet1)
Application.OnTime Now + TimeValue("00:00:01"), "!Sheet1.RunClock"

Related

Workbook.close Cannot run the marco "https://wfp-mysharepoint.com............"

Background infromation:
This workbook is on shared point and I'm trying to run a Marco in Desktop app.
The objective on the marco is to close the workbook automatically after 10 mins. I want to ensure that users do not keep the workbook open without using it.
However, when I run the below code, I get an error message
"Cannot run the marco "https://wfp-mysharepoint.com............" This Macro may not be available in this workbook or marcos maybe disabled.
The code is as follows:
Private Sub Workbook_Open()
Picktime
End Sub
Sub Picktime()
savetime = Hour(Now) & ":" & Minute(Now) + 1 & ":" & Second(Now)
Application.OnTime savetime, "Please_close"
End Sub
Sub Please_close()
ThisWorkbook.Close (True)
End Sub
The procedure Workbook_Open is an event (that runs automatically) on opening of the workbook and therefore has to be in the scope of the workbook ThisWorkbook. Other events that are worksheet related are located in the scope of their worksheet.
All other code should go into a normal module. Especially code that needs to be found by Application.OnTime can only be located in a normal module.
So in ThisWorkbook:
Private Sub Workbook_Open()
Picktime
End Sub
In a normal module:
Public Sub Picktime()
Dim SaveTime As Variant
SaveTime = Now() + TimeValue("00:01:00")
Application.OnTime EarliestTime:=SaveTime, Procedure:="Please_close"
End Sub
Public Sub Please_close()
ThisWorkbook.Close SaveChanges:=True
End Sub
Note that it is easer to add one minute to Now() by using Now() + TimeValue("00:01:00")

Getting error for macro when moving to a different sheet

Im getting a Run-time error '1004':
Select method of range class failed
This only happens when I move away from the sheet (Bet Angel) that has the macro running to another sheet. The code is below:
Sub DeleteStatus()
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
I want the macro to run even if I am moving between different sheets in the workbook.
Since you are moving to another worksheets, you need to activate or select the target worksheet first.
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
If you want to go back to your original worksheet, you can activate it at the end of DeleteStatus()
Like this
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
Sheets("your original worksheet").Activate
End Sub
If you don't want to use activate, then you need to dim the worksheet first.
Like this
Sub DeleteStatus()
Dim ws As Worksheet
Set ws = Sheets("Bet Angel")
ws.Range("O6:O50").ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub

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

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

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