Hello,
I have a problem to have a sheet periodically recalculated every minute.
I have a simple procedure that calls itself every minute that goes basically like this:
The biggest problem with that construction is that if a user leaves excel open (with another workbook after it closes this workbook), this OnTime method will try to open the file back after a minute passes. A lot of problems are caused by this ranging from always reopening file, to runtime errors based on circumstances.
To tackle this i have decided that I need to move the procedure into Microsoft Excel Objects section under Sheet 1 and have it run for the first time when sheet gets activated, and reset the OnTime if the user deactivates the sheet. So I created the following :
And I have run into another problem, that is that if I have the procedure Recalculate in the regular macro module, the variables RecalculateRun and RecalculateTime does not pass into the Sheet1 and I cannot close the OnTime prolerly,
Or
If i move everything into the Sheet1 code space the sub Recalculate cannot be found when the OnTime triggers.
I suspect a problem with properly defining global variables, or global subroutines or something, but can't figure it out on my own.
Please advise.
I have a problem to have a sheet periodically recalculated every minute.
I have a simple procedure that calls itself every minute that goes basically like this:
Code:
Public Sub Recalculate(ByVal IDcko As Integer)
Calculate
RecalculateTime = Now + TimeValue("00:01:00")
Application.OnTime earliesttime:=RecalculateTime, _
procedure:="'Recalculate 1'"
End Sub
The biggest problem with that construction is that if a user leaves excel open (with another workbook after it closes this workbook), this OnTime method will try to open the file back after a minute passes. A lot of problems are caused by this ranging from always reopening file, to runtime errors based on circumstances.
To tackle this i have decided that I need to move the procedure into Microsoft Excel Objects section under Sheet 1 and have it run for the first time when sheet gets activated, and reset the OnTime if the user deactivates the sheet. So I created the following :
Code:
Dim RecalculateRun As Boolean
Dim RecalculateTime As Variant
Private Sub Worksheet_Activate()
Call Recalculate(0)
End Sub
Private Sub Worksheet_Deactivate()
If RecalculateRun Then
RecalculateRun = False
Debug.Print "About to inactivate OnTime with " & RecalculateTime
Application.OnTime earliesttime:=RecalculateTime, _
procedure:="'Recalculate 1'", Schedule:=False
End If
End Sub
And I have run into another problem, that is that if I have the procedure Recalculate in the regular macro module, the variables RecalculateRun and RecalculateTime does not pass into the Sheet1 and I cannot close the OnTime prolerly,
Or
If i move everything into the Sheet1 code space the sub Recalculate cannot be found when the OnTime triggers.
I suspect a problem with properly defining global variables, or global subroutines or something, but can't figure it out on my own.
Please advise.