I have code in Excel VBA which I like to run several times a second.
Before I used this method :
Application.On Time Now + Timevalue("00:00:01")
Although this method can only handle seconds (and no milliseconds).
For that reason I replaced this timer with a milliseconds timer (see code below). Although since I integrated the milliseconds timer with my other Excel VBA code it sometimes gives this error: "Unexpected error 35010".
There are several reasons mentioned for this error:
VBA compile Error: unexpected error 35010
I did test the code on two computers and on both it returns the error. I also did test on Excel Pro Plus 2019 and Excel Pro Plus 2016. Both 64 bit versions. Both return the error.
Since I expected it was due to a corrupt file, I did made a new workbook and copied all VBA code into this workbook. I started with empty sheets and copied all formulas from the old workbook to the new workbooksheets. I did this twice although these files still return the error.
The error always occurs directly after I start the code. The error does not refer to a specific line of code (in yellow).
Somtimes the error occurs, while with exactly the same file and contents the error does not occur.
This is the code for the timer:
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, 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
This is example code how I use the timer:
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
Does anyone know what the cause of the error can be?
Thanks a lot!