PDA

View Full Version : Control with time



ndendrinos
02-03-2007, 06:33 PM
This example works on different computers differently in the sense that on my desktop (old & slow) the duration on the "spin" takes "x" seconds while on a laptop (new & fast) the spin ends much too soon.
Not sure why but it does.
How can I then control the duration of the spin using seconds (time) instead of

For N = 1 To 300 thus having the same visual effect on all computers.
The desired time is 3 seconds

Thank you

asingh
02-03-2007, 08:39 PM
Hi,

Attached is a sheet, I am getting three (3) seconds, for a "SPIN". It should not vary to much, unless the old systems, have processors with really slow clock speeds...! To check for consistency, run the module with most resource eating applications turned off...!


regards,

Asingh

rbrhodes
02-03-2007, 10:13 PM
Try this - it works off of the system clock and is VERY simple so cycles should be correct.


Sub spin1()
Dim timing As Long
timing = Timer
Do Until Timer = timing + 3
Randomize
[B2] = Int(Rnd() * 50 + 1)
Loop
End Sub



Cheers,

dr

Bob Phillips
02-04-2007, 04:06 AM
How about sticking in a wait



Wait Now + TimeSerial(0,0,3)

mdmackillop
02-04-2007, 05:20 AM
It's better practice not to select cells in your VBA code, unlress necessary.
Try
Option Explicit

Sub spin1()
Dim Timing As Long, Cel As Range

Set Cel = Range("B2")

With Range("E5")
.Value = Now
.Copy
.PasteSpecial Paste:=xlPasteValues
End With

Application.CutCopyMode = False
Cel.Activate
Randomize
With Cel
Timing = Timer
Do Until Timer > Timing + 3
.Value = Int(Rnd() * 50 + 1)
Loop
End With

With Range("E6")
.Value = Now
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
Cel.Activate
Application.CutCopyMode = False

End Sub

ndendrinos
02-04-2007, 06:58 AM
Thank you all for helping out.

asingh : It works fine but I was able to control the duration of the spin through the "For N = 1 To 300" so in my mind at least the spin will vary in duration on different computers ... if I'm wrong pls correct me.

rbrhodes : tried your suggestion and there is a problem with it ... the spin does not stop ... ??

xld : I too thought along those lines but could not introduce the "Wait Now" in the code ... maybe you could post an example


mdmackillop : Took your good advise and changed it to accomodate my project
( the final will be longer as there are six cells that need to do the same routine ) :

Sub Spin()
Call spin1
Call spin2
End Sub
_______________________________________________
Sub spin1()
Dim Timing As Long, Cel As Range
Set Cel = Range("B2")
Cel.Activate
Randomize
With Cel
Timing = Timer
Do Until Timer > Timing + 3
.Value = Int(Rnd() * 50 + 1)
Loop
End With

End Sub
_____________________________________________________
Sub spin2()
Dim Timing As Long, Cel As Range
Set Cel = Range("C2")
Cel.Activate
Randomize
With Cel
Timing = Timer
Do Until Timer > Timing + 3
.Value = Int(Rnd() * 50 + 1)
Loop
End With

End Sub



Is there a better way to do this?
Also: I took the liberty of deleting parts of your code. Am I defeating the purpose in doing so?

mdmackillop
02-04-2007, 07:50 AM
Option Explicit

Sub spin()
Dim Cel As Range
For Each Cel In Range("B2:G2")
Call DoSpin(Cel)
Next
End Sub

Sub DoSpin(Cel As Range)
Dim Timing As Long
Cel.Activate
Randomize
With Cel
Timing = Timer
Do Until Timer > Timing + 3
.Value = Int(Rnd() * 50 + 1)
Loop
End With
End Sub

ndendrinos
02-04-2007, 08:40 AM
Just great ... thank you mdmackillop
Out of courtesy to the others that have volunteered their help I will mark this one solved in a couple of days

Best Regards,
Nick