No me apaga el timer. Application.OnTime

Tengo una macro que es un timer para control de hornos. Pero cuando llega a la sub para apagar el timer mediante

Application.OnTime Earliesttime:=CuentaRegresiva2, procedure:="ProgramaCuenta2", Schedule:=False

Envia el msg error Run-time error '1004': Method 'OnTime' of object '_Application' failed

El código es:

Public Cuentai As Integer
Dim ejecutando As Boolean
Dim NO As String
Public CuentaRegresiva2 As Date

Sub setcrono()
ejecutando = True
[F4] = [K4]
Call ProgramaCuentaRegresiva2
End Sub

Sub ProgramaCuentaRegresiva2()
CuentaRegresiva2 = Now + TimeValue("00:00:01")
Application.OnTime CuentaRegresiva2, "ProgramaCuenta2", Schedule:=True
End Sub
Sub ProgramaCuenta2()

Dim Cuenta2 As Range
Dim s As Integer
Dim hora As String

Set Cuenta2 = [F4]
Cells(4, 7).Interior.ColorIndex = 2
Cuenta2.Value = Cuenta2.Value - TimeSerial(0, 0, 1)

If Cuenta2 <= 0 Then
      hora = Format(Now, "HH:mm:ss")
           If hora > "15:15:00" Then
               Call EnviarEmail
         End If
    For i = 0 To 10
      Call SonidoControlado
        For q = 0 To 2500
           Cells(4, 7).Interior.ColorIndex = 3
         Next
         q = 0
         For q = 0 To 2500
         Cells(4, 7).Interior.ColorIndex = 2
        Next
  Next
Cells(4, 7).Interior.ColorIndex = 3
NO = Sheet1.Range("B4").Text
MsgBox "Terminó el Tiempo de Proceso Traveler " + NO, vbExclamation
Cuenta2.Value = [K4]
Call apagarcrono
Exit Sub

End If

Call ProgramaCuentaRegresiva2

End Sub

Sub apagarcrono()
ejecutando = False
Application.OnTime Earliesttime:=CuentaRegresiva2, procedure:="ProgramaCuenta2", Schedule:=False
End Sub

Respuesta
2

H o l a:

No entiendo por qué envía el error.

Lo estuve probando y lo que pude detectar es que hay que poner los mismos parámetros en ambos métodos.

Cambia esta macro

Sub ProgramaCuentaRegresiva2()
CuentaRegresiva2 = Now + TimeValue("00:00:01")
Application.OnTime CuentaRegresiva2, "ProgramaCuenta2", Schedule:=True
End Sub

Por esto:

Sub ProgramaCuentaRegresiva2()
    CuentaRegresiva2 = Now + TimeValue("00:00:01")
    Application.OnTime CuentaRegresiva2, "ProgramaCuenta2", , True
End Sub

Cambia esta macro:

Sub apagarcrono()
ejecutando = False
Application.OnTime Earliesttime:=CuentaRegresiva2, procedure:="ProgramaCuenta2", Schedule:=False
End Sub

Por esto:

Sub apagarcrono()
    ejecutando = False
    Application.OnTime CuentaRegresiva2, "ProgramaCuenta2", , False
End Sub

Prueba y me comentas.


':)
S a l u d o s . D a n t e A m o r
':) Si es lo que necesitas. Recuerda valorar la respuesta. G r a c i a s.

Gracias Dante Amor por tu pronta respuesta.

Hice lo que me dijiste y siguió enviando el mismo msg de error pero, invertí tus instrucciones y funcionó. Te dejo el código.

Public Cuentai As Integer
Dim Cuenta2 As Range
Dim ejecutando As Boolean
Dim NO As String
Public Iniciar2 As Date

Sub setcrono()
ejecutando = True
Set Cuenta2 = [K4]
[F4] = [K4]
Call ProgramaCuentaRegresiva2
End Sub

Sub ProgramaCuentaRegresiva2()
Iniciar2 = Now + TimeValue("00:00:01")
Application.OnTime Earliesttime:=Iniciar2, procedure:="ProgramaCuenta2", Schedule:=True
End Sub
Sub ProgramaCuenta2()


Dim s As Integer
Dim hora As String

Set Cuenta2 = [F4]
Cells(4, 7).Interior.ColorIndex = 2
Cuenta2.Value = Cuenta2.Value - TimeSerial(0, 0, 1)

If Cuenta2 <= 0 Then
hora = Format(Now, "HH:mm:ss")
If hora > "15:30:00" Then
Call EnviarEmail
End If
For i = 0 To 1
Call SonidoControlado
For q = 0 To 2500
Cells(4, 7).Interior.ColorIndex = 3
Next
q = 0
For q = 0 To 2500
Cells(4, 7).Interior.ColorIndex = 2
Next
Next
Cells(4, 7).Interior.ColorIndex = 3
NO = Sheet1.Range("B4").Text
MsgBox "Terminó el Tiempo de Proceso Traveler " + NO, vbExclamation
Call apagarcrono
Exit Sub

End If

Call ProgramaCuentaRegresiva2

End Sub

Sub apagarcrono()
ejecutando = False
On Error Resume Next
Application.OnTime Earliesttime:=Iniciar2, procedure:="ProgramaCuenta2", Schedule:=False
End Sub

Para preguntar como ejecutar esta parte de la macro

Call SonidoControlado
For q = 0 To 2500
Cells(4, 7).Interior.ColorIndex = 3
Next
q = 0
For q = 0 To 2500
Cells(4, 7).Interior.ColorIndex = 2
Next


Hasta que presionen una tecla ¿necesito abrir otra pregunta?

Sí, crea una nueva pregunta, recuerda valorar esta pregunta.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas