How to deal with “runtime error 50290” when using SetTimer API?

前端 未结 2 1845
暖寄归人
暖寄归人 2021-01-24 14:07

I\'ve run into this error when trying to make a stopwatch timer in Excel. Here\'s a simple test code. Create an empty Excel workbook with a button. And assign a macro to it:

相关标签:
2条回答
  • 2021-01-24 14:56

    I found using OnTime with current time will cause your code to run on the main thread immediately or fail. I found this better because you only need to trap the OnTime error.

    Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal 
    dwTimer As Long)
        On Error GoTo BeforeExit
        Application.OnTime Now, "updateWorksheet"
    BeforeExit:
    End Sub
    
    Sub updateWorksheet
        Range("A1") = "test"
    End Sub
    
    0 讨论(0)
  • 2021-01-24 15:07

    I was able to solve it with

    On Error Resume Next
    

    at the beginning of TimerProc. Some more (in Russian) or less related links.

    Or probably even better:

    Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
        On Error GoTo BeforeExit
        Range("A1") = "test"
    BeforeExit:
    End Sub
    
    0 讨论(0)
提交回复
热议问题