Loop Macro until Userinput, continue if no input

cfoley220

New Member
Joined
Jun 8, 2016
Messages
1
Hello!

I can't figure out how to make a code loop until user input and continue if there is no input. I don't want to use Ctrl + Break, because I am interested in how many times the code ran and how long it took.

Is there a way to have the code pause and basically ask the user to stop? However, I want it to continue if I receives no input. I plan to run this code over long periods of time while I am away from my computer and want to be able to come back and stop it.
I don't mind waiting for a loop to finish to tell it to stop.


This is my code right now (Set to run just three times) :

Sub CalculateRunTime_SecondsBUDDY()


Dim StartTime As Double
Dim SecondsElapsed As Double


'Remember time when macro starts
StartTime = Timer


Let x = 0
Do While x < 3
Calculate
x = x + 1
Loop


'Determine how many seconds code took to run
SecondsElapsed = Round(Timer - StartTime, 2)


'Notify user in seconds
MsgBox "This BUDDY code ran " & x & " times successfully in " & SecondsElapsed & " seconds. Wow!", vbInformation


End Sub


My current theory is to have the line: Do While x < 3 become Do While x < y.
And somehow user input can change y, so to start I set it on a high number, but when I want it to stop, I can enter 0.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
There are probably better ways to do this, but does the user input have to be directly into excel?

Could you build into your loop a section that opens a .txt file and reads the first line into a string, then closes it. If the variable value is (for example) "Stop", it exits the loop, otherwise it continues. All you then need to do is leave the .text file open in notepad - when you want the macro to stop, type "Stop" on the first line and save the file.
 
Upvote 0

Forum statistics

Threads
1,215,945
Messages
6,127,840
Members
449,411
Latest member
adunn_23

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