Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...