Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JON JON is offline
external usenet poster
 
Posts: 1
Default Exiting a VBA Macro by user intervention

I have an issue where I am calling a control within an Excel spreadsheet.
The control basically fires off a command that runs X number of itterations
.. I want the user to be able to control the number of itterations because
the larger the number of itterations the more accurate is the answer. The
problem is if the user inputs a value too large for the number of
itterations the computer will chug away for hours and the only way to exit
the VB macro is hitting escape to stop it. My question is how can I put in
a control in the spreadsheet that will stop the macro during the execution
of it. I have tried several things and it apperas that while the
spreadsheet is under control of the macro the user has no ability to regain
control until the macro has finished, errors out, or the user hits esc to
break in.
I know I could limit the number of itterations by checking the input but I
really want the user to be able to let the program run for extended periods
of time. What would be best is if the user could click on a start control
and fire the macro, then click on a stop control to stop the macro and
display the results.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Exiting a VBA Macro by user intervention

JON

Put a module level variable in your module

Dim bStop as Boolean

If the control is on a worksheet, put it in the worksheet module. If on a
userform, the userform module. It goes before any procedures in the module.

Create a commandbutton to stop the macro with this code in its click event

Private Sub cmdStop_Click()
bStop = True
End Sub

Change cmdStop to whatever you name your control.

In the sub that loops, check the value of bStop and add a DoEvents

Private Sub cmdStart_Click()
....
For i = 1 to Max
DoEvents
If bStop Then
bStop = False
Exit Sub 'or other clean up activities that you may need
End If

'loop guts
Next i

End Sub

One caveat: If your looping sub is in a standard module and not in the same
module as your control event subs, then dimension bStop in the standard
module with the Public keyword

Public bStop As Boolean


--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"JON" wrote in message
s.com...
I have an issue where I am calling a control within an Excel spreadsheet.
The control basically fires off a command that runs X number of

itterations
. I want the user to be able to control the number of itterations because
the larger the number of itterations the more accurate is the answer. The
problem is if the user inputs a value too large for the number of
itterations the computer will chug away for hours and the only way to exit
the VB macro is hitting escape to stop it. My question is how can I put

in
a control in the spreadsheet that will stop the macro during the execution
of it. I have tried several things and it apperas that while the
spreadsheet is under control of the macro the user has no ability to

regain
control until the macro has finished, errors out, or the user hits esc to
break in.
I know I could limit the number of itterations by checking the input but I
really want the user to be able to let the program run for extended

periods
of time. What would be best is if the user could click on a start control
and fire the macro, then click on a stop control to stop the macro and
display the results.



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
Exiting Excel MEAD5432 Excel Discussion (Misc queries) 2 December 7th 07 11:05 PM
Excel to Oracle without user intervention Illya Teideman Excel Discussion (Misc queries) 0 March 23rd 07 03:25 PM
disable user running macro from Tools Macro Steve Simons Excel Discussion (Misc queries) 4 September 28th 06 06:28 AM
Exiting Worksheet Evan Excel Discussion (Misc queries) 1 February 11th 05 06:06 PM
make sure macro be loaded without user intervention Soe Excel Programming 3 July 25th 03 05:02 PM


All times are GMT +1. The time now is 08:02 AM.

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

About Us

"It's about Microsoft Excel"