Share via

How to stop application.onTime loop

Anonymous
2021-11-12T15:46:26+00:00

Sub hey()
Range("C1:C59") = Range("B1:B59").Value
Application.OnTime Now + TimeValue("00:00:10"), "Hey"
End Sub

This code not stopping by stop button. how to stop this code.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2021-11-12T18:53:06+00:00

Try pressing Ctrl+Break to pause code execution, then select Run > Reset in the Visual Basic Editor.

In the future, do it like this:

  1. At the top of a standard module, declare a variable:

Public dtmNext as Date

  1. Change your macro as follows:

Sub hey()
Range("C1:C59") = Range("B1:B59").Value
dtmNext = Now + TimeValue("00:00:10")
Application.OnTime dtmNext, "Hey"
End Sub

  1. Copy the following code into the ThisWorkbook module:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime dtmNext, "Hey", , False
End Sub

This stops the OnTime loop when you close the workbook.

Was this answer helpful?

5 people found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful