Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

excel - Delay macro to allow events to finish

While attempting to access an external API function set from within a macro I found it necessary to add in a delay to allow time for the external API to process a selection.

Implementing this caused a bit of difficulty as using Application.Wait or Application.Sleep wouldn't work.

Searching online led me to try using GetTickCount or timeGetTime - both of which failed to work for me as the delay function failed to exit.

The question thus is how can I implement this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead I implemented the delay using the functions Now and DateAdd:

Function Delay(Seconds As Long)
    Dim StopTime As Date: StopTime = DateAdd("s", Seconds, Now)
    Do While Now < StopTime
        DoEvents
    Loop
End Function

Updated below for millisecond precision

Function Delay(Seconds As Single)
    Dim StopTime As Single: StopTime = Timer + Seconds
    Do While Timer < StopTime
        DoEvents
    Loop
End Function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...