Timer Tick every 1 second

Solstice

New Member
Joined
Mar 12, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I am trying to make a Timer executing a sub every second.
I need a delay of 1 second then executing sub.

VBA Code:
Public TimerValue As Variant
Public TimerTick As Variant
Private Sub Window_Open()
    TimerValue = 1
    TimerTick = Now
    Do While TimerValue <> 0
       TimerTick = TimerTick + TimeSerial("00:00:01")
       'Delay of 1 sec
        Execute()
    Loop

End Sub
Private Sub Stop_Click()
    TimerValue = 0
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Perhaps call a timer function - that way you can re-use the code easily. This works in Access, so I'm guessing Excel vba has the same built in Timer function. By using a Single, you can specify fractions of a second.
VBA Code:
Sub pause(sngSecs As Single)
Dim endTime As Long

endTime = Timer
Do Until Timer > endTime + sngSecs
Loop

End Sub
 
Upvote 0
Perhaps call a timer function - that way you can re-use the code easily. This works in Access, so I'm guessing Excel vba has the same built in Timer function. By using a Single, you can specify fractions of a second.
VBA Code:
Sub pause(sngSecs As Single)
Dim endTime As Long

endTime = Timer
Do Until Timer > endTime + sngSecs
Loop

End Sub
Not understanding how
VBA Code:
endTime = Timer
works
 
Upvote 0
Here is a suggestion :

VBA Code:
Option Explicit

Sub MacroRun()
'Connect this macro to a CommandButton to start the timer or
'make reference to this macro in the Workbook_Open module

    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    Application.OnTime Now + TimeValue("00:00:01"), "Macro1"
    End
    Application.DisplayAlerts = True
End Sub

Sub Macro1()

'Your macro code here

MacroRun

End Sub

Sub stopMacros()
'Create a CommandButton attached to this macro if you want to stop
'the timer from running.

    Exit Sub
End Sub
 
Upvote 0
I just noticed that after modifying this in the past to accept single, the other variable should also be single. See if explanatory notes help.
VBA Code:
Sub pause(sngSecs As Single)
Dim endTime As Single

'example of Timer value at run time: 43828.66. Thus endTime = 43828.66
endTime = Timer

'IIRC, Timer is built in function that gets number of seconds after midnight so
'if sngSecs = 5 then do until Timer returns a value greater than 43828.66 + 5
Do Until Timer > endTime + sngSecs
Debug.Print Timer
Loop

End Sub
 
Upvote 0
I am trying to make a Timer executing a sub every second.
I need a delay of 1 second then executing sub.

The only way to achieve this is using the OnTime method, like @Logit suggested, but the code has to renew the scheduled time each time, otherwise it will not work.
Furthermore, the repetition can be aborted by canceling the last scheduled time. This should always be done before closing the workbook.

This goes in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ScheduleCancel
End Sub


This goes in a standard module:
VBA Code:
Option Explicit

Private Type TLocals
    ScheduledTime As Double         ' <<< storage for scheduled date & time
    Macro         As String         ' <<< storage for macro to be scheduled
End Type
Private this As TLocals

Public Sub ScheduleCancel()
    On Error Resume Next
    Excel.Application.OnTime EarliestTime:=this.ScheduledTime, Procedure:=this.Macro, Schedule:=False
End Sub

Public Sub ScheduleTimeToRun()
    With this
        .ScheduledTime = VBA.Now + VBA.TimeValue("00:00:01")    ' <<<< interval of one second
        .Macro = "RunMeEverySecond"                             ' <<<< change macro name to suit
        Excel.Application.OnTime .ScheduledTime, .Macro
    End With
End Sub

Public Sub RunMeEverySecond()   ' <<<< change macro name to suit
    '
    '  your code
    '
    ScheduleTimeToRun           ' <<<< renew scheduled time
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top