0
votes

I would like some help to understand why the function doSomething is not running.

Calculation mode is on Automatic

Sub Test()
    check = 0
    
    Debug.Print "Timer_1: " & Now()
    
    Call runFromNow("doSomething", "00:00:05")
    Do While check = 0
        DoEvents
    Loop
    
    Debug.Print "Timer_2: " & Now()
End Sub

Sub runFromNow(myProcedure As String, Optional myTime As Variant = "00:00:15")
    If myProcedure <> "" Then
        Application.OnTime Now + TimeValue(myTime), myProcedure
        Debug.Print "runFromNow: Activated at " & Now + TimeValue(myTime)
    End If
End Sub

Sub doSomething()
    check = 1
End Sub
1
While this might not be the best way to run the timer like you intend to, you can solve this by making check into a global variable. Right now it doesn't work because the check inside Test() is not the same check that is updated in doSomething() - Marcucciboy2
I highly recommend to use Option Explicit to avoid such issues. This would force you to declare check a second time if it is not declared public. And so you would immediately see that it was only declared private. Especially for beginners Option Explicit forces you to write better code. - Pᴇʜ
Marcucciboy2, if this is not the best way, what should I use instead? - Elio Fernandes

1 Answers

3
votes

You could try to get this one done with declaring a public variable.

I wrote this short test for giving you an example:

Public Check As Long

Sub Test()
    
    Call Test_Dam
    
    Debug.Print Check

End Sub

Sub Test_Dam()

    Check = 1

End Sub

Your Debug.print will give you the 1 you need ;)