tags:

views:

68

answers:

2

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

Thanks.

+4  A: 

Use the Wait method:

Application.Wait(Now + TimeValue("0:00:01"))
Ben S
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
Keng
I'm not exactly sure what you mean by that, but I speculate you want `DoEvents` as demoed here http://www.dailydoseofexcel.com/archives/2005/06/14/stopwatch/
Ryan Shannon
+1  A: 

Add this to your module

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Call it in your macro like so:

Sub Macro1()
'
' Macro1 Macro
'
Do
    Calculate
    Sleep (1000) ' delay 1 second

Loop
End Sub
Buggabill
Why not use the VBA method to do this rather than using kernel32.dll?
Ben S
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
Buggabill
There is also another VBA application (ERS) that I use that has a _very_ limited subset of the language.
Buggabill
+1, because `Sleep()` lets you specify wait times of less than 1 second. `Application.Wait` is sometimes too granular.
BradC