Exit a loop with a button click

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,126
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All

So basically, i was wondering if i could exit a loop if i made a command button.

I want to call this within my other code.
But wish to click a command button if possible to end it.


Code:
Sub nagg()
    For I = 1 To 100
        Call Application.Speech.Speak("Attention required")
        Application.Wait (Now + TimeValue("00:00:30"))
    Next I
End Sub

cheers

dave
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi Dave,

What if you press break key (break or ctrl + Break) or escape key ?


Regards,
DILIPandey
 
Upvote 0
Using Wait or Sleep locks you out until the time period has finished. You can use CTRL+BREAK to interrupt the macro, but Excel will not accept input from the keyboard or mouse whilst paused using either method. If users are not aware of this feature, it can look like your Excel code has hung.
You can use a loop instead where you can pause or exit VBA code

Give following a try & see if any help to you:

Place all code in your worksheet (or Forms) Code Page

Code:
Dim StopIt As Boolean

Private Sub CommandButton1_Click()
StopIt = True
End Sub



Sub nagg()
    Dim RunningTime As Double
    Dim TimerStart As Double
    
    TimerStart = Timer
    
    Do Until StopIt
        RunningTime = (Timer - TimerStart) / 24 / 60 / 60
        If (Format(RunningTime, "ss") Mod 30) = 0 Then Application.Speech.Speak ("Attention required")
        DoEvents
    Loop
    StopIt = False
End Sub

Code should keep looping until you press your button & give speech message every 30 seconds.

Note the StopIt variable – this MUST sit at the very TOP of the code page OUTSIDE any procedure.

Hope Helpful

Dave
 
Last edited:
Upvote 0
Hi DILIPandey

I was unaware that i could use CTRL break, i knew about escape, is there any difference between them?
Thnaks for your help

DMT32
Thats awsome mate, the code works great, i will get this intergrated into my project.
Thankyou very much for taking the time to help me.

Really appriciate both of your help.

Dave
 
Upvote 0

Forum statistics

Threads
1,222,615
Messages
6,167,065
Members
452,093
Latest member
JamesFromAustin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top