A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
I've found the following code which works really well.
I can't agree, it works... but well?
The basic is just this little sub in a regular module:
Sub MyTimerSub()
'Tell Excel to execute this sub in one second again
Application.OnTime Now + TimeSerial(0, 0, 1), "MyTimerSub"
End Sub
The comment says all, you can e.g. add a line
Range("A1") = Range("A1") + 1
and you see "a counter running" in A1, but it never stops. Try that, don't worry, if you want to stop the execution, place a breakpoint on a line in the sub, wait until the code stops and click the reset button in the VBA menu.
To do that with code is also simple, have a look:
Option Explicit
Dim TimerEnabled As Boolean
Sub MyTimerSub()
If Not TimerEnabled Then Exit Sub
Range("A1") = Range("A1") + 1
'Call this sub in one second again
Application.OnTime Now + TimeSerial(0, 0, 1), "MyTimerSub"
End Sub
Sub StartTimer()
TimerEnabled = True
MyTimerSub
End Sub
Sub StopTimer()
TimerEnabled = False
End Sub
Sub ResetTimer()
TimerEnabled = False
Range("A1") = 0
End Sub
But Excel is not designed for such gadgets, if you edit a cell, the code stops. If you try to work with Excel (copy cells, etc.) you can't do that properly. No way to prevent that.
It is funny to write and play around with such code, you can learn a lot of things, but using in a real application... never ever.
Andreas.