View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Command Button Request

The only way I can think of to terminate running code is to modify the loop
(I assume the running code is in some sort of Loop structure) to test the
value of a Public variable, whose value would be set by your command button.
For example, declare a Public variable named Done as a Boolean in the
module containing the loop code and write your command button code as

Sub CommmandButton1_Click()
Done = True
' clear the ranges
End Sub

Then in the module containing the procedure that needs to be told to quit,
declare a variable as a Public variable (outside of and before any procedure
in the module):

Public Done As Boolean

Then test this variable in your loop code. E.g,

Public Sub YourSub()
Done = False
Do Until (your_normal_terminate_condition) Or (Done = True)
'''''''''''''''''''''''''''''''''
' your code here
'''''''''''''''''''''''''''''''''
Loop
''''''''''''''''''''''''''''''''''''''''
' cleanup code as required
'''''''''''''''''''''''''''''''''''''''
End Sub

This assumes that the Button is from the Controls toolbar. If it is from the
Forms tool bar, declare the variable as described above, and just enter
Done = True
to the macro that is already assigned to the button. Note that in both
cases, the declaration of the variable Done must be Public, outside of and
before any procedure in the module. If you declare the variable within a
procedure, it won't work.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Saxman" wrote in message
...
Sorry, but I did not realise there was a live thread with the same title
'Command Button' which is where my original request ended.

I would like to create a dual task command button that would stop some
code running and clear data on another worksheet. Or would it be best to
keep these operations separate?

Clearing the data could be done with a macro assigned to the command
button, but I'm not sure how I would go about stopping the code running.

Any help appreciated thanks.