'Application.OnTime - Worksheet_Calculate - Interference
Application.OnTime only works when Excel is in ready mode. When you use Worksheet_Calculate() event Excel is never in ready mode since the worksheet is constantly calculating (in this case). A suggestion is to create a module scoped variable to hold the scheduled time and use Worksheet_Calculate() to execute the scheduled task. My goal was to log a row of data using Application.OnTime every minute but that never worked since it was interfering with the Worksheet_Calculate() events in my other sheets. Need help placing this block of code in my Worksheet_Calculate() event and to log the time on another worksheet every minute.
Option Explicit
Public DoOnTime As Date
Private Const SchedulePeriod As Date = "00:01:00" ' 1 minute
Private Sub Worksheet_Calculate()
Dim cel As Range, Capture As Range, Ws1 As Worksheet, Ws2 As Worksheet
Set Ws1 = Worksheets("Dashboard")
Set Ws2 = Worksheets("Log")
If Now() >= DoOnTime Then
On Error GoTo safeexit
Application.EnableEvents = False
Set Capture = Ws1.Range("A39:Q39") 'Capture this row of data
With Ws2 'Record the data on this worksheet
Set cel = .Range("A2") 'First timestamp goes here
Set cel = .Cells(.Rows.Count, cel.Column).End(xlUp).Offset(1, 0)
cel.Value = Now
cel.Offset(0, 1).Resize(Capture.Rows.Count,
Capture.Columns.Count).Value = Capture.Value
End With
DoOnTime = Now() + SchedulePeriod
Debug.Print "Triggered at", Now(), "Scheduled at", DoOnTime
End If
safeexit:
Application.EnableEvents = True
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
