doevents & application.wait

Frequent Contributor

hello everyone

i want to understand how doevents works, for animating chart why we should use do events? what i understand doevents is used to stop the macro code from executing, but how this relates to below chart for animating chart, when code reaches to doevents it stops executing, so why then chart refreshes & updates the values in chart?? if i use only application.wait chart popups one time

 

please can someone explain to me how "doevents" works in this code (chart also attached below) 

 

Sub partialanimation()

Dim i As Integer

Application.Calculate
For i = 1 To 12
DoEvents

Application.Wait (Now + TimeValue("00:00:01"))
Range("c21").Value = i
Application.Calculate
DoEvents
Next i

End Sub

animating chart.png

 

 

1 Reply

@chahine 

From DoEvents function:

"DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent."

As a result, macro execution slows down slightly, but it doesn't stop.

On the other hand, from Application.Wait method (Excel):

"The Wait method suspends all Microsoft Excel activity and may prevent you from performing other operations on your computer while Wait is in effect. However, background processes such as printing and recalculation continue."

So a macro comes to a complete standstill during the time specified by Application.Wait.