cancel message while sub running

  • Thread starter Thread starter tracktraining
  • Start date Start date
T

tracktraining

Hi All,

I would like to create a cancel message that appears while a sub is running.
So, when the user hit the cancel button on the message box (or userform),
then the sub will exit and start the program from the beginning. And if the
user doens't hit the cancel button, then the sub will continue to work as is.

Any help is much appreciated.

thanks,
tracktraining
 
I thought application.ONKEY would work, but my test failed.

alternatively, with my test code, run it and the put 1 into cell A1

Option Explicit
Sub cancelled()
Dim i As Long
Dim bCancel As Boolean
bCancel = False
Do
Application.StatusBar = i
i = i + 1
If Range("A1") = 1 Then bCancel = True
DoEvents ' releases control back to PC - ie user can enter data into
the spreadsheet
Loop Until bCancel Or i >= 1000000
if bCancel then msgbox "User interrupted!"
End Sub
 
Can you tell me where I am supposed to put that code?

I have a userform and the code is outline as below:

Private Sub Graph_button_Click()
' obtains data and creates graphs base on user inputs
end sub

Private Sub Userform_Initialize()
' initialize the starting userform (making everything blank)
end sub


When the user hit the graph button, that's when i want the "Please wait
while report is being generated. If you want to cancel is transaction, please
click on the cancel button below." vbcancel , to appear.

if the user doesn't hit the cancel button after the report is generated,
then the message (or userform) will close.

But if the user hit the cancel button, then i want to stop the code from
running.

thanks again for helping.
 
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
 
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!
 
yes, it makes sense. But I still can't get the cancel function to work (i.e.
cancel program when the cnacel button is pushed).
 
code for the cancel message is not working as expected. I will need to find a
different approach. Thanks for helping.
 
Back
Top