Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Stopping a userform subroutine

I have a looping sub triggered by a userform button. I'm trying to figure
out how to stop that same sub with another button on the same userform. It's
kind of ugly just hitting escape. Any ideas? Is there a standard way? I
tried 'loop until' and making the second button achieve the 'until'
condition but that didn't work - I can't trigger the second button at all.



  #2   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Stopping a userform subroutine

Hi

If one sub is running behind the userform, you can not run another
sub.

Post the macro, so we can find a solution.

Regards,
Per

On 11 Jan., 23:40, "teepee" wrote:
I have a looping *sub *triggered by a userform button. I'm trying to figure
out how to stop that same sub with another button on the same userform. It's
kind of ugly just hitting escape. Any *ideas? Is there a standard way? I
tried 'loop until' and making the second button achieve the 'until'
condition but that didn't work - I can't trigger the second button at all..


  #3   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Stopping a userform subroutine

There are a couple of ways to stop a macro once it begins, but none of them
are really desirable. One, of course, is Ctrl + Alt + Delete, that's the
"When all else fails" one. Another is Ctrl + Break, which is mostly used to
halt a macro for debugging or when a continuous loop is accidentally
initiated. Then there are the designed breaks like using a message box
(MsgBox) function to temporarily halt a macro without going into break mode,
or using "Stop" in your code to pause the macro in edit mode. But generally
speaking, once you start a macro it will run to completion unless you use one
or more of the above.

"teepee" wrote:

I have a looping sub triggered by a userform button. I'm trying to figure
out how to stop that same sub with another button on the same userform. It's
kind of ugly just hitting escape. Any ideas? Is there a standard way? I
tried 'loop until' and making the second button achieve the 'until'
condition but that didn't work - I can't trigger the second button at all.




  #4   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Stopping a userform subroutine

This worked for me.

Tim

'********************************
Option Explicit

Dim bStop As Boolean

Private Sub CommandButton1_Click()
Dim x As Long

bStop = False
x = 0
Do While Not bStop And x < 30
Me.CommandButton1.Caption = "Running: " & x
Application.Wait Now + TimeValue("0:00:01")
x = x + 1
DoEvents 'allows the other button code to run if clicked
Loop
Me.CommandButton1.Caption = "Stopped"

End Sub

Private Sub CommandButton2_Click()
bStop = True
End Sub

'*********************************



"teepee" wrote in message
...
I have a looping sub triggered by a userform button. I'm trying to figure
out how to stop that same sub with another button on the same userform.
It's kind of ugly just hitting escape. Any ideas? Is there a standard way?
I tried 'loop until' and making the second button achieve the 'until'
condition but that didn't work - I can't trigger the second button at all.





  #5   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 449
Default Stopping a userform subroutine

Hi

Userform with Commandbutton1 (start) and Commandbuton2 (stop):

Option Explicit

Dim Stopit As Boolean

Private Sub CommandButton1_Click()
Dim i As Long, J As Long
Stopit = False
For i = 1 To 1000000
For J = 1 To 10000
'just spending time here ;-)
Next
DoEvents
If Stopit = True Then Exit Sub
Me.Caption = i
Next
End Sub

Private Sub CommandButton2_Click()
Stopit = True
End Sub

Trick here is "DoEvents" which roughly means "listen to the operating system
before you continue".

HTH. Best wishes Harald


"teepee" wrote in message
...
I have a looping sub triggered by a userform button. I'm trying to figure
out how to stop that same sub with another button on the same userform.
It's kind of ugly just hitting escape. Any ideas? Is there a standard way?
I tried 'loop until' and making the second button achieve the 'until'
condition but that didn't work - I can't trigger the second button at all.






  #6   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Stopping a userform subroutine

thanks will give it a go


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
halt a subroutine question teepee[_3_] Excel Discussion (Misc queries) 3 December 28th 08 09:23 PM
SUBROUTINE HELP biker man Excel Discussion (Misc queries) 1 July 28th 07 04:06 PM
How to use this subroutine on multiple cells? Big Ian Excel Worksheet Functions 4 February 14th 06 02:01 PM
How do I exit a macro subroutine? John Excel Worksheet Functions 1 January 15th 06 02:08 AM
How to pass a workshhet name as a parameter into a subroutine ? yigalb Excel Discussion (Misc queries) 4 January 9th 05 10:28 AM


All times are GMT +1. The time now is 11:51 PM.

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"