When and how to use DoEvents (SOLVED)

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
I get this feeling that DoEvents is a pretty significant function to know about. I'm afraid I have checked it out in the help file and I am more confused. Basically, this is all the help file says about it:

Yields execution so that the operating system can process other events.

After that, it goes into a description of what it does that is way over my head. I was wondering what the practical uses are for DoEvents. When is it most commonly used?

_________________
...................****************** EXCEL/VB NEWBIES ARE MY <font color="red"> FAVORITE</font>! *****************...................<font color="blue">The only dumb question is the one that isn't asked. Pass on what you have learned.
This message was edited by phantom1975 on 2003-01-26 12:16
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Phantom1975,

One example is if we use a userform to inform users the present status of a batchjob that constantly need to be updated, i e a counter or similar.

Please mail back if You want a working sample.

Kind regards,
Dennis
 
Upvote 0
It sounds like it is used to allow a macro to run in the background so that other tasks can be performed. For example, if I have the following:

Application.Wait (Now + TimeValue("0:00:10"))

I am unable to do anything until the time has expired. So if I change it to:

DoEvents
Application.Wait (Now + TimeValue("0:00:10"))


I would think that I can still type or do whatever else I need to do. I know that Application.Ontime would be the preferred method, however, I am trying to understand the DoEvents a bit better.

Is this basically does it just make the macro run in the background?
 
Upvote 0
I use it for splash screens, and "Please wait..." screens for lengthy code operations.

The code below shows a form for the specified time. All you need to do is show the form in the WorkBook_Open event.

This was pretty much ripped off from the help file

Private Sub UserForm_Activate()
Dim PauseTime, Start, Finish, TotalTime
PauseTime = 4 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Set end time.
TotalTime = Finish - Start ' Calculate total time.
Unload Me

End Sub

Wait screens can be more complex as they depend on other things than time.

Hope this helps
 
Upvote 0
Option Explicit

Sub DontDoEvents()
'hog up all of the processor time allocated to Excel
'use CNTL BREAK to get in this loop
Do
Loop
End Sub

Sub DoDoEvents()
'share some Pentium here(or AMD)
Do
DoEvents
Loop
End Sub

Sub TryAndRunThisWhileOneOfTheAboveAreRunning()
MsgBox "Thanks for allowing me to do this event."
End Sub

'you obviously will not be able to run this when the first
'procedure is running. But you will be able to when the
'second is running. DoEvents can cause you some trouble
'if not used properly. Make sure your code does not call the
'procedure which used DoEvents to begin with. You will be
'rebooting! Also, if you find yourself using this function
'quite often in VBA as opposed to other languages which provide
'API wrapper timers, you may want to learn about using API timers
'and the Callback Function. Ivan has posted some good examples
'on this board.
 
Upvote 0
Actually the DoEvents statement will put the execution of your macro on hold to "process events". These so called events are for example the user scrolling down the sheet, the user clicking somewhere... If the DoEvents is called several times will little heavy code inbetween (for example in a loop), the workbook will be quite responsive to the user actions.
What you have to take-away though is that DoEvents is a bit dangerous. It gives the user the impression she/he can interact with the worksheet when in fact the macro is still running (and not in the background! no no! it is simply sharing its execution time with the user actions).
This implies that:
- The macro will be slower with doevents
- If the macro uses context variables like Activesheet, selection, activeworkbook and so one, you can be sure to create substantial errors.

You can have a look at this article on DoEvents if you want more info and do a little quiz ;)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top