+ Reply to Thread
Results 1 to 5 of 5

why do i need "Do Event" twice in my code

  1. #1
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    why do i need "Do Event" twice in my code

    Hi all. Hope everyone is staying safe with this virus running around. I had a few hours on my hands this week so I thought it would be good to develop a model that simulates an infection in a group of moving people. Its a fun little model that allows you to play with different infection rates in a population and shows a dynamic chart of the infection spreading. Sorry for the morbid topic but it seemed relevant this week. One of the things I've been watching on the news was the desire to reduce the peak and this is being done by way of social isolation so I wanted to see if this model could replicate the flattening of the peak. It does and you might be interested to play with the model but I still have a coding issue and it is this.

    I want to see the chart moving so I need to leave screenupdating on but that alone wasn't enough to allow the chart to update so I had to add a doevent line but one line wasn't enough I had to put it in twice!!. As you can see from the code below I've been trying different ways to get the screen updating. Its working now with the present code but only with the double DoEvent. Why do I have to run this line of code twice???

    If anyone is interested in playing with the model I think its a bit of fun.

    There are three variables to set, the number of time intervals a person remains sick after catching the virus, the speed at which people move around the space and the distance between people before the virus is transferred. The model starts off with 199 uninfected people (blue dots) and 1 infected person (red dot). they are all set on a particular direction at the beginning of the model. The infected person remains infected for the number of time periods set before being cured. NO ONE DIES AND ALL DOTS ARE EVENTUALLY CURED. If an infected person comes into contact (with in the set distance) of an uninfected person then the uninfected person becomes infected too. Once cured a person can not catch the virus again. Play with it by setting different variables in the three yellow cells and running the simulation button.

    BUT PLEASE EXPLAIN WHY I NEED TWO DOEVENTS!!

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Happy with my advice? Click on the * reputation button below

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: why do i need "Do Event" twice in my code

    Most likely you have two main loops in your code, and each DoEvents method are responsible for each main loop. Also, Application.ScreenUpdating is not necessary since you intend to show the animation.

    DoEvents should go inside the loops to be clear for its purpose.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: why do i need "Do Event" twice in my code

    Thanks. I'll relocate them inside the loop and see how that goes

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437

    Re: why do i need "Do Event" twice in my code

    I noticed the need for 2 doevents ever since 2016 was released. I can not give you a technical reason but without the refreshing of the screen does not appear to happen.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: why do i need "Do Event" twice in my code

    Thanks Andy. Must be another one of life's mysteries

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. ActiveX Textbox - "Enter" or "Return" key event - possible?
    By ironfelix717 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2018, 09:56 PM
  3. Excel "loses" an event during complex event processing.
    By tfurnivall in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2017, 11:32 AM
  4. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  5. [SOLVED] Code Clean-Up: Delete "False" worksheet created when "Cancel" is chosen in Input Box
    By Kenny Blackwell in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 08-31-2015, 10:00 AM
  6. Running Code After "Opening File" Event Has Triggered Data Connection Refresh
    By david.nicholls in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2013, 07:22 AM
  7. WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab
    By blackworx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-02-2012, 06:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1