ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Stop / Pause a Macro using Pause button (https://www.excelbanter.com/excel-programming/413204-re-stop-pause-macro-using-pause-button.html)

scott56hannah

Stop / Pause a Macro using Pause button
 
Paul,

Thanks for that.....I followed that approach and now have it working as needed

Scott

" wrote:

Hi
1. In your normal code module declare this public variable at the top

Public StopIt As Boolean

2. Change ProcessEvents macro to this

Sub ProcessEvents()
'This routine will process events and when complete needs to pause and
wait for user input
Dim i As Long
StopIt = False
Do While intloopCount < 10
i = 0
blnAllowPauseContinueEvent = False
Do While i < (intloopCount * 500)
UserForm1.LoopCountLabel.Caption = "Loop Count " &
intloopCount & " Display Number " & i
UserForm1.Repaint
i = i + 1
If StopIt Then Exit Do
DoEvents
Loop
intloopCount = intloopCount + 1
If StopIt Then Exit Do
DoEvents
Loop
End Sub

3. Change PauseButton_Click to this

Private Sub PauseButton_Click()
If UserForm1.PauseButton.Caption = "Pause" Then
UserForm1.PauseButton.Caption = "Continue"
MsgBox "Button was Paused and is now Continue"
StopIt = True
Exit Sub
End If
If UserForm1.PauseButton.Caption = "Continue" Then
UserForm1.PauseButton.Caption = "Pause"
MsgBox "Button was Continue and is now Pause"
StopIt = False
Call ProcessEvents
End If
End Sub

regards
Paul

On Jun 26, 11:55 pm, scott56hannah
wrote:
Yes....and it did not operate as needed....I still cannot get the Pause
button to function or respond while the macro is running...which is exactly
what I want to pause...

Thanks



"Bob Phillips" wrote:
Did you try the answer that you got at VBAExpress?


--
__________________________________
HTH


Bob


"scott56hannah" wrote in message
...
Hi,


I would like to stop / pause a macro that is running using a Pause button
on
a form...


Some of the code is copied below....


Currently when you start the macro it continues through until then end
even
if the pause button is selected...it seems only to recognise the pause
button
when it gets to the end of the processing....


Is there anyway to get the Pause button recognised while the macro is
running ?


Private Sub PauseButton_Click()
If UserForm1.PauseButton.Caption = "Pause" Then
UserForm1.PauseButton.Caption = "Continue"
MsgBox "Button was Paused and is now Continue"
Exit Sub
End If


If UserForm1.PauseButton.Caption = "Continue" Then
UserForm1.PauseButton.Caption = "Pause"
MsgBox "Button was Continue and is now Pause"
Call ProcessEvents
End If


End Sub
Private Sub StartButton_Click()
intLoopCount = 1
UserForm1.PauseButton.Caption = "Pause"
Call ProcessEvents


End Sub
Private Sub UserForm_Activate()
UserForm1.LoopCountLabel.Caption = "Loop Count " & intLoopCount & "
Display Number "
End Sub


Sub ProcessEvents()
'This routine will process events and when complete needs to pause and
wait for user input
Dim i As Long


Do While intLoopCount < 10


i = 0
blnAllowPauseContinueEvent = False
Do While i < (intLoopCount * 500)


UserForm1.LoopCountLabel.Caption = "Loop Count " & intLoopCount
& " Display Number " & i
UserForm1.Repaint
i = i + 1


Loop


intLoopCount = intLoopCount + 1
Loop


End Sub


Thanks
Scott- Hide quoted text -


- Show quoted text -





All times are GMT +1. The time now is 05:43 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com