A code question from a rookie

S

SuitedAces

Please help me with this

I grabbed the following code from a website, it can be used as a timer
It works exactly as intended.

My question is which of the variables or procedure names need to b
changed to have two or more timers like this coexist in the sam
workbook.

I highlighted in bold what I am guessing needs to be changed.


*********************************************************
Public Declare Function SetTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
ByVal HWnd As Long, ByVal nIDEvent As Long) As Long

Public *TimerID* As Long
Public *TimerSeconds* As Single

Sub *StartTimer*()
*TimerSeconds* = 1 ' how often to "pop" the timer.
*TimerID* = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressO
*TimerProc*)
End Sub

Sub *EndTimer*()
On Error Resume Next
KillTimer 0&, *TimerID*
End Sub

Sub *TimerProc*(ByVal HWnd As Long, ByVal uMsg As Long, _
ByVal nIDEvent As Long, ByVal dwTimer As Long)
'
' The procedure is called by Windows. Put your
' timer-related code here.
'
End Sub
**********************************************************
Here are some comments that were included with the code from th
website.

The procedure TimerProc will be called by Windows every time the time
pops. You can name this procedure anything you want, but you mus
declare the argument variables exactly as shown in the example. If yo
change the name of the procedure, be sure to change the name in th
call to SetTimer as well.

nIDEvent The value returned by SetTimer to the TimerID variable. I
you have made more than one call to SetTimer, you can examine th
nIDEvent argument to determine which call SetTimer to resulted in th
procedure being called.

*THANK YOU FOR YOUR HELP
 
A

Andrew B

Hi
Try this if what you are trying to do is just delay the running of a
procedure.


Application.Wait Now + TimeValue("00:00:05")


Place this line where you want the delay to occur.

HTH

Andrew Bourke
 
L

Leith Ross

Hello SuitedAces,

The API (Applications Programming Interface) timer is identified by th
variable nIDEvent in the Function Declaration SetTimer. This is a
unsigned integer value (positive number only). The APi is a very usefu
and powerful interface with few safegaurds. It is important t
understand what these calls are doing or you can crash your system.

Example of 2 Timers:
Sub StartTimer1()
TimerSeconds = 1 ' how often to "pop" the timer.
TimerID = SetTimer(0&, 1&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub

Sub StartTimer2()
TimerSeconds = 1 ' how often to "pop" the timer.
TimerID = SetTimer(0&, 2&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub

You should always kill the timer after it is used to free syste
resources. This kind of housekeeping is automatic in VB but not th
API.

Sincerely,
Leith Ros
 
S

SuitedAces

THANK YOU FOR THE REPLIES.

I have finally been able to make this work consistently , flashing
either
two cell or two labels independently.

I was able to get OnTime working with one object flashing , but when I
went
to using two timers I ran into all sorts of strange results.

So I used the API method
It turns out that I do need to change everything that I outlined in
bold.
On top of that ...
In my experiment sheet I have four button one to start and one to stop
each timer.
I found out I needed to disable the start button after the first press
then
reenable it with the stop button. Multiple presses of the start buttons
gave
me some irratic behaviour.

I have the timers being killed in code but I think I will also add it
to the sheet close event as a fail safe.

*Thank You for the help*
 
L

Leith Ross

Hello SuitedAces,

I didn't know how you were going to start the timers, but in any case I
should have mentioned that calling the start routine again before the
timer has finished will restart the cycle. Sorry about that oversight.

Sincerely,
Leith Ross
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top