Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default how to stop program with loop by click "Cancel" button

Hi, everyone
Right now I wanna do a time-comsuming loop in excel programming, for
example:

for i = 0 to 100000
for j = 0 to 10000
'my parts
doEevents
next
next

during run time, I still wanna use a "Cancel" button to let user have choice
to stop it during run time?
how to do it, Pls advice


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default how to stop program with loop by click "Cancel" button

Hi,
In your for 'j' loop you could show a user form at the start. Then
before the next statement test it for cancel value and then kill the
form. The form would load and unload for each time it loops, to the
user it would appear to be one continous form. If 'Cancel' is clicked
then you could Exit Sub or use a goto statement to exit the loop.
HTH--Lonnie M.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 123
Default how to stop program with loop by click "Cancel" button

At module level define a Public Variable useCanceled as Boolean.
(This should be outside any 'Sub' - 'End Sub'

In the commandbutton click event set this variable to true.
Private Sub CommandButton1_Click()
userCanceled = True
End Sub

Then modify your sub code as under. It also shows declaration of the
public variable:

Public userCanceled As Boolean

Sub test()
For i = 1 To 100000
If userCanceled Then
userCanceled = False
Exit For
End If
For j = 1 To 10000
'your code
DoEvents
If userCanceled Then Exit For
Next j
Next i
End Sub

I am assuming that your code is in a module. If not then \
'Public userCanceled As Boolean' should be moved to a module.

Sharad


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Click on cell (or "button"), takes you to appropriate information ahwelch Excel Worksheet Functions 1 June 3rd 09 08:13 PM
How do I stop "global" hyperlinks changing to "local" links? Em Excel Worksheet Functions 2 August 26th 08 01:18 PM
Complex if test program possible? If "value" "value", paste "value" in another cell? jseabold Excel Discussion (Misc queries) 1 January 30th 06 10:01 PM
commnd button - when i rt click, "assign macro" isnt an option... kevin[_3_] Excel Programming 1 November 12th 04 09:52 PM
Close workbook with "Cancel=TRUE" in the BeforeClose()" Wellie[_3_] Excel Programming 1 October 16th 04 09:46 PM


All times are GMT +1. The time now is 11:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"