Stop / Pause a Macro using Pause button

S

scott56hannah

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
 
S

scott56hannah

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
 
P

paul.robinson

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
 
S

scott56hannah

Paul,

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

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top