loop problem
Hi Teepee,
If you use the following you'll see the number of times the procedure
is looping each time it is run (recorded in column G & only up to
where E1 = 1). So there isn't an update being made every 0.2 seconds,
but every time the loop is passed through.
Option Explicit
Sub count()
Dim tim1, tim2, diff As Double
Dim lCounter As Long
Range("A1:E1,G:G") = ""
tim1 = Timer 'sets the time the process started
Do Until Range("E1") = 1
tim2 = Timer
diff = tim2 - tim1
If diff = 1 Then
Range("e1") = Range("e1") + 1
tim1 = Timer
ElseIf diff = 0.8 Then
Range("d1") = Range("d1") + 1
ElseIf diff = 0.6 Then
Range("c1") = Range("C1") + 1
ElseIf diff = 0.4 Then
Range("b1") = Range("b1") + 1
ElseIf diff = 0.2 Then
Range("a1") = Range("a1") + 1
End If
Range("G1").Offset(lCounter, 0) = lCounter + 1
lCounter = lCounter + 1
Loop
End Sub
HtH,
JF.
On 31 Dec, 09:31, "teepee" wrote:
Hiya
I have a looping sub that takes 5 cells and every 0.2 seconds adds to their
values, dealing with one at each successive 0.2 second interval , so that by
the end of each second every cell has been visited and increased by the
macro once. In other words at 0.2 seconds A1 is increased, at 0.4 seconds B1
is increased, 0.6 seconds C1 is increased 0.8 seconds D1 is increased and
then at 1 second E1 is increased. And then it all loops.
The problem I have is that I want to increase the cell values by the value
of just one each time. By using the = instruction it increases each cell by
many hundreds on each loop cycle (except for the last one in the series.)
However if I just use = instead, the system ignores the instruction
altogether - it doesn't seem sensitive enough to register.
Any solutions involving lots of do loops disrupt the timer function
altogether, which is very vulnerable to any system resource reallocations..
Does anyone see a simple elegant solution that doesn't require heavier
processing? Many thanks.
Sub count()
Range("A1") = 0
Range("B1") = 0
Range("C1") = 0
Range("D1") = 0
Range("E1") = 0
tim1 = Timer * *'sets the time the process started
Do
tim2 = Timer * 'sets current time
diff = tim2 - tim1 * 'derives the number of seconds since process started,
refreshing on every loop
If diff = 1 Then
Range("e1") = Range("e1") + 1
tim1 = Timer * 'restarts process by resetting number of seconds since
process started to zero
ElseIf diff = 0.8 Then
Range("d1") = Range("d1") + 1
ElseIf diff = 0.6 Then
Range("c1") = Range("C1") + 1
ElseIf diff = 0.4 Then
Range("b1") = Range("b1") + 1
ElseIf diff = 0.2 Then
Range("a1") = Range("a1") + 1
End If
*Loop
End Sub
|