毫秒计时器返回意外错误35010

问题描述

我在Excel VBA中有代码,我希望每秒运行几次。

在使用此方法之前:

Application.On Time Now + Timevalue("00:00:01")

尽管此方法只能处理几秒钟(无毫秒)。

因此,我将此计时器替换为毫秒计时器(请参见下面的代码)。 尽管自从我将毫秒计时器与其他Excel VBA代码集成在一起以来,它有时会出现以下错误:“意外错误35010”。

提到此错误的原因有几个:

VBA compile Error: unexpected error 35010

我确实在两台计算机上测试了代码,并且在两台计算机上都返回了错误。 我还在Excel Pro Plus 2019和Excel Pro Plus 2016上进行了测试。都是64位版本。两者都返回错误

由于我希望这是由于文件损坏导致的,所以我制作了一个新的工作簿并将所有VBA代码复制到该工作簿中。我从空工作表开始,然后将所有公式从旧工作簿复制到新工作表。我做了两次,尽管这些文件仍然返回错误

启动代码后,错误总是直接发生。该错误并不涉及特定的代码行(黄色)。

有时会发生错误,而文件内容完全相同则不会发生错误

这是计时器的代码

Option Explicit

Private Declare PtrSafe Function SetTimer Lib "user32" _
(ByVal hWnd As LongPtr,_
ByVal nIDEvent As LongPtr,_
ByVal uElapse As Long,_
ByVal lpTimerFunc As LongPtr) As LongPtr

Private Declare PtrSafe Function KillTimer Lib "user32" _
(ByVal hWnd As LongPtr,_
ByVal nIDEvent As LongPtr) As Long

Private m_TimerID As LongPtr

'Note: The duration is measured in milliseconds.
' 1,000 milliseconds = 1 second
Public Sub StartTimer(ByVal Duration As Long)
    'If the timer isn't already running,start it.
    If m_TimerID = 0 Then
        If Duration > 0 Then
            m_TimerID = SetTimer(0,Duration,AddressOf TimerEvent)
            If m_TimerID = 0 Then
                MsgBox "Timer initialization Failed!"
            End If
        Else
            MsgBox "The duration must be greater than zero."
        End If
    Else
        MsgBox "Timer already started."
    End If
End Sub

Public Sub StopTimer()
    'If the timer is already running,shut it off.
    If m_TimerID <> 0 Then
        KillTimer 0,m_TimerID
        m_TimerID = 0
    Else
        MsgBox "Timer is not active."
    End If
End Sub

Public Property Get TimerIsActive() As Boolean
'A non-zero timer ID indicates that it's turned on.
    TimerIsActive = (m_TimerID <> 0)
End Property

这是我如何使用计时器的示例代码

Public CountSomething As Variant
Private timerRunning As Boolean


Sub UPDATECLOCK()


''Application.Calculation = xlManual


If Not timerRunning Then
            CountSomething = 0
            r = 0
            StartTimer 100
            timerRunning = True
End If
 

End Sub

Sub STOPCLOCK()


            StopTimer
            timerRunning = False



End Sub

Sub TimerEvent()


On Error GoTo ErrHandler

    'force code to run on the main thread instead of Windows api thread:
    If timerRunning Then Application.OnTime Now,"updateWorksheet"
    Exit Sub

ErrHandler:
    Debug.Print Err.Number

End Sub

Sub updateWorksheet()
    
    'call code to process here
    'or this example code in this case:
    
    CountSomething = CountSomething + 1
    Worksheets("Sheet1").Cells(2,9).Value = CountSomething
    
    'write 1 in cel A1 to stop code running:
    knop = ActiveSheet.Cells(1,1)

    If knop = 1 And timerRunning Then
        StopTimer
        timerRunning = False
    End If
    
    
End Sub

有人知道错误的原因是什么吗?

非常感谢!

解决方法

代码中存在一些概念错误:使用OnTime Now ...等同于立即调用updateWorksheet。 timerRunning变量是多余的,可以使用TimerIsActive函数。 可以(应该)将updateWorksheet的代码插入TimerEvent中,没有理由检查计时器是否处于活动状态。仅当计时器处于活动状态时,才调用该例程。 该代码是等效的

Sub UPDATECLOCK()

    If Not TimerIsActive Then
        CountSomething = 0
        r = 0
        StartTimer 100
    End If
 
End Sub

Sub STOPCLOCK()

    StopTimer

End Sub

Sub TimerEvent()

    CountSomething = CountSomething + 1
    Worksheets("Sheet1").Cells(2,9).Value = CountSomething
    'write 1 in cel A1 to stop code running:
    If ActiveSheet.Cells(1,1) = 1 Then 'why ActiveSheet?
        StopTimer
    End If
    
End Sub