cancel message while sub running
sorry, i guess i didnt' make my question/comment clear.
When i pasted the code (with the DoEvents uncomment) in my graph_click sub,
then run the program. The userform2 appears but i am unable to click the
Cancel button. When i click on the cancel button, the program still runs
though my sub and at the end then it will show "user cancelled", but the
program already completed so the point of having the cancel button is lost
=(.
so i created a userform2 and pasted the form code as in Patrick's response.
then i pasted the code form userform 1 into my Graph_button_click sub (but
was unable to hit the cancel button on the userform2, explained above) . My
sub Graph_button_click is pretty long so i won't paste it into this message.
Below is an idea of what i have in the sub. I guess i don't know how to use
the code from step 2 (see Patrick Molloy message) with my graph_button_click
code. Please advise.
sub Graph_button_click
obtain data
do calculation
create graphs
end sub
2)
Option Explicit
Private Sub CommandButton1_Click()
UserForm2.Show vbModeless
Dim i As Long
For i = 1 To 100000
UserForm2.Label2.Caption = i
DoEvents
If UserForm2.bCancelled Then Exit For
Next
If UserForm2.bCancelled Then
MsgBox "user cancelled"
else
MsgBox "finished normally"
End If
Unload UserForm2
End Sub
thanks for your help!
--
Learning
"smartin" wrote:
' DoEvents - is there where i add in my code - obtain data,
DoEvents is a one word instruction that tells VBA to pause execution and
process Events -- such as a user clicking a "Cancel" button or redrawing
a label caption. Once events are processed, execution resumes. As such,
DoEvents is important here and you should uncomment it. Without it your
loop will fly around at the speed of light and not give Windows a chance
to process the Click event.
In practice you will not notice the "pause", but your program should be
more responsive to the clicks you intend to handle.
tracktraining wrote:
I tried your code and incorporate it into my code and it didn't work. when
userform2 appears, i can unable to click cancel - my code runs as is.
2)
Option Explicit
Private Sub CommandButton1_Click()
UserForm2.Show vbModeless
Dim i As Long
For i = 1 To 100000
UserForm2.Label2.Caption = i
' DoEvents - is there where i add in my code - obtain data,
calculation, create graphs (call other functions/subs)?
If UserForm2.bCancelled Then Exit For
Next
If UserForm2.bCancelled Then
MsgBox "user cancelled"
else
MsgBox "finished normally"
End If
Unload UserForm2
End Sub
|