VBA Code stuck in a loop

arkie1979

New Member
Joined
Jan 30, 2019
Messages
2
Hi,

Im using this code below but im stuck in the loop until i press ESC. Can someone help me write into this code that it stops when ESC is pressed

Sub rrrr()
Do
button24hr
Application.Wait Now + TimeSerial(0, 0, 5)
BUTTONNOW
Application.Wait Now + TimeSerial(0, 0, 5)
ActiveWorkbook.refreshall
Application.Wait Now + TimeSerial(0, 0, 10)
Loop
End Sub


Thanks.....VBA is very new to me
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Welcome to the Board!

You have not told your macro when to stop, so it never will!
Have a look here: https://corporatefinanceinstitute.com/resources/excel/study/vba-do-loop/

Note that if you do create a loop that just has "Do" and "Loop" with no "Until" portion, you need something in the code to tell it when to exit, i.e.
Code:
If x= "somevalue" then Exit Do

Or at the very least, place a loop counter and exit when you hit some really large number, so you are not caught in an infinite loop, i.e.
Code:
Do
    'do some stuff here
    i = i + 1
    If i > 1000 Then
        MsgBox "Exiting loop after 1000 iterations"
        Exit Do
    End If
Loop
 
Upvote 0
Welcome to the Board!

You have not told your macro when to stop, so it never will!
Have a look here: https://corporatefinanceinstitute.com/resources/excel/study/vba-do-loop/

Note that if you do create a loop that just has "Do" and "Loop" with no "Until" portion, you need something in the code to tell it when to exit, i.e.
Code:
If x= "somevalue" then Exit Do

Or at the very least, place a loop counter and exit when you hit some really large number, so you are not caught in an infinite loop, i.e.
Code:
Do
    'do some stuff here
    i = i + 1
    If i > 1000 Then
        MsgBox "Exiting loop after 1000 iterations"
        Exit Do
    End If
Loop

Thankyou for your help but i need my code to run constantly on some screens it will be played on, so i cant put a counter on it. But if someone has it on their screen and want to exit it, i would like them to press a button (ESC ideally) and it stops and they can close the spreadsheet with ease. At present im doing CRT+Break and then pressing end, but im worried someone may press 'debug' and mess with the code
 
Upvote 0
Thankyou for your help but i need my code to run constantly on some screens it will be played on, so i cant put a counter on it. But if someone has it on their screen and want to exit it, i would like them to press a button (ESC ideally) and it stops and they can close the spreadsheet with ease. At present im doing CRT+Break and then pressing end, but im worried someone may press 'debug' and mess with the code

Whilst you module code is running then you aren't able to do anything else in your Workbook - the code is just going to keep running until you stop it running.

Probably what you REALLY need is to use worksheet or workbook events to trigger some code when certain things happen in your Workbook.

If you explain what you are trying to achieve we may be able to help
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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