Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 -- - K Dales "Maxi" wrote: 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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
hey sorry !!
Its working......... I reduced the counter of (While ctr < 600000 And Not (StopIt)) from 600000 to 2500 Thanks a lot |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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. -- - K Dales "Maxi" wrote: hey sorry !! Its working......... I reduced the counter of (While ctr < 600000 And Not (StopIt)) from 600000 to 2500 Thanks a lot |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks for the tip
|
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Interrupting Calculation Iterations | Excel Discussion (Misc queries) | |||
Interrupting A Worksheet Calculation | Excel Worksheet Functions | |||
Interrupting an endless loop | Excel Programming | |||
Interrupting a loop | Excel Programming | |||
Interrupting an add line | Excel Programming |