Interrupting a Loop

M

Maxi

Private Sub CommandButton1_Click()

Dim ctr As Long
Dim StartNum As Integer

StartNum = 0

Range("B2").Select

While StartNum <= 9
Range("B2") = StartNum
StartNum = StartNum + 1
While ctr < 600000
ctr = ctr + 1
Wend
ctr = 1
If StartNum = 10 Then
StartNum = 0
End If
Wend

End Sub


I have two buttons in sheet1, "Start" and "Stop"
When I click the "Start" button, it runs the above code and increments
value in cell B2 but the mouse loses focus. I want to interrupt the
above code when I click the "Stop" button but it does not allow me to
click anywhere as the code is in Loop.

How can I click the "Stop" button?

I got a reply from forums to add "doevents" below ctr = ctr + 1 line to
get access to the stop button but the initial macro pauses to enable
events on the form. I am looking to multitask both, the loop and the
events.

Value in cell B2 should keep on incrementing and the moment user clicks
on the Stop button, it should stop.

Thanks
Maxi
 
K

K Dales

See example below. In addition to simply stopping the loop, the code for
button2 could do other kinds of processing. However, when you say
"multitask": the 2 subs will not and cannot run simultaneously, you always
have to "interrupt" one sub for another, or any event procedure, to occur.
But you can make the two work together, with one sub calling the other or
using the "interrupt" method I am using here to perform some kind of
processing (including signalling the loop to stop as I am doing here).


Public StopIt As Boolean

Sub Button1_Click()
Dim ctr As Long
Dim StartNum As Integer

StartNum = 0
StopIt = False

Range("B2").Select

While StartNum <= 9 And Not (StopIt)
Range("B2") = StartNum
StartNum = StartNum + 1
While ctr < 600000 And Not (StopIt)
DoEvents
ctr = ctr + 1
Wend
ctr = 1
If StartNum = 10 Then
StartNum = 0
End If
Wend

End Sub


Sub Button2_Click()

StopIt = True

End Sub
 
M

Maxi

I am sorry but it does not help.

It controls the focus so that I can click on the Stop button. But the
number does not increment in cell B2.

What I want is when I click START button, the value in cell B2 should
increment by 1 like 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
................... and the moment I click STOP button, the value in
cell B2 should stop incrementing and give any one value from 0 to 9.

But reading your post, I feel it is impossible. I replied to this post
just to ensure that there is no confusion between both of us.
 
M

Maxi

hey sorry !!


Its working......... I reduced the counter of (While ctr < 600000 And
Not (StopIt)) from 600000 to 2500

Thanks a lot
 
K

K Dales

Rather than incrementing a counter I find it is usually better to use a timed
loop for things like this:

Dim StopTime as Date
StopTime = Now() + TimeValue("00:02:00") ' two second timed loop
While Now() < StopTime ...
....
WEnd

The timing is not precise, but you have better control than when you just
increment a counter: when you use a counter or fixed number of iterations the
time taken to complete the loop can vary a lot depending on everything else
happening on your PC.
 

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