View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Sharad Sharad is offline
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!