I have an Excel worksheet that has the following macro. I'd like to loop it every second but danged if I can find the function to do that. Isn't it possible?
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
'Here I want to wait for one second
Loop
End Sub
Use the Wait method:
or (for Excel 2010 and later):
this works flawlessly for me. insert any code before or after the "do until" loop. In your case, put the 5 lines (time1= & time2= & "do until" loop) at the end inside your do loop
instead of using:
i prefer:
because it is a lot easier to read afterwards.
Most of the presented solutions use Application.Wait, which does not take in account the time (miliseconds) already elapsed since the currend second count started, so they have an intrinsic imprecision of up to 1 second.
The Timer approach is the best solution, but you have to take in account the reset at midnight, so here is a very precise Sleep method using Timer:
USE THIS TO TEST ANY SLEEP FUNCTION: (open debug Immediate window: CTRL+G)
Try this :
Add this to your module
Or, for 64-bit systems use:
Call it in your macro like so: