Thread: loop problem
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
teepee[_3_] teepee[_3_] is offline
external usenet poster
 
Posts: 107
Default loop problem

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