DoEvent

  • Thread starter Thread starter Billabong
  • Start date Start date
B

Billabong

I am a neophyte programmer, I heard DoEvents can be used to run tw
sequence at almost the same time. But I dont know how to use it.
Anyone kind enough to show me how it works? A sample coding will do an
a bit expanation how it works.

Thanks
 
Billabong

DoEvents releases the processor from the currently executing macro for
whatever is in the processors queue. You normally should not need it, but
there are times when you do. Here's an example: Suppose you have a macro
that increments the value of a cell until the user hits a button. You might
set that up like this

Public bStop As Boolean

Sub IncrementCell()

Do
Range("A1").Value = Range("A1").Value + 1
DoEvents
Loop Until bStop

End Sub

Sub StopIncrement()
'assigned to button

bStop = True

End Sub

If you didn't have the DoEvents in the first sub, the button would never be
available for the user to hit because the IncrementCell would never release
any processor time to anything else. Clicking the button to StopIncrement
will not do anything because StopIncrement will be in the processor's queue
waiting for IncrementCell to finish, which of course it never would.
 
Excel isn't multitasking and won't run two procedures at the same time.


as far as DoEvents, it is explained pretty well in Help:

"Yields execution so that the operating system can process other events."
If that is what you mean by two sequence then it can do that, but the second
sequence, the operating system tasks, are outside your control.

for i = 1 to 1000
doevents
' other actions
Next

might be useful if the code were making visible changes and they were not
being updated on the screen because the code is in a tight loop. Doevents
is usually used primarily when problems are encountered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top