+ Reply to Thread
Results 1 to 6 of 6

Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

  1. #1
    Registered User
    Join Date
    05-02-2018
    Location
    CT, USA
    MS-Off Ver
    2007/10
    Posts
    23

    Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

    Hi,
    I have a macro that opens and modifies a lot of different files. Currently the only way I can make this work is by using a lot of Application.OnTime commands, which is making the code extremely unwieldly.
    for some background, the reason I do this is as follows (simplified):

    I Start with a template that I input data on, then execute the Macro.
    The code goes like this
    Please Login or Register  to view this content.
    If I try to run the code like that however, it gets an error when trying to modify the downloaded file, because it doesn't recognize that the file has been opened, a subscript out of range error. (if i run through line by line the code works because the system processes that the file has been opened)
    so what I ended up doing is this:
    Please Login or Register  to view this content.
    This works, but is making it very hard to keep track of everything since it cant all be called in one sub.

    My googling makes it seem like I should be able to do what follows, but it doesn't work.
    Am I misinterpreting what DoEvents does / is there any alternative to get something like this?

    Please Login or Register  to view this content.
    Any advice is much appreciated.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

    I think we need to see the rest of your code, especially the part that downloads the file. Is it an asynchronous process? There may be another way to run a loop to check to see if the download is done before moving on. Otherwise you may want to just use Sleep or Wait instead of setting a timer.

    Normally once VBA starts to run, the Excel user cannot do anything until the code finishes. Excel will ignore anything the user tries to do. DoEvents returns control to Excel long enough to process any queued up user actions, such as mouse clicks or typing text, then returns to code execution. It is often used, for example, in a loop to allow a user to continue working in a worksheet while the code runs.

  3. #3
    Registered User
    Join Date
    05-02-2018
    Location
    CT, USA
    MS-Off Ver
    2007/10
    Posts
    23

    Re: Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

    Hi, sorry for the delay - I really do appreciate the help.
    I have been running a LOT of tests and I'm starting to think this is an SAP nuance, not an excel one...

    Based on your input, and what I have been reading, can you tell me if I'm understanding correctly the following:
    1. DoEvents is for user inputs - not for allowing excel to process functions like opening a large file.
    2. Sleep or Wait can be used to allow excel to process functions like opening a large file before moving on to the next line of code.
    3. excel will not wait for a line of code to be finished executing before moving on to the next one
    ex: this may fail (but not always?)
    Please Login or Register  to view this content.
    ex) this should always work at a high enough xxx value
    Please Login or Register  to view this content.
    Again, thank you very much for the assistance.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

    1. DoEvents is for user inputs - not for allowing excel to process functions like opening a large file.

    Correct

    2. Sleep or Wait can be used to allow excel to process functions like opening a large file before moving on to the next line of code.

    Incorrect. A single line of code, such as opening a file, is an atomic function. When the line starts to execute, it maintains control until it finishes, and then when it is done the next line executes.

    The exception to this is a call to an external process. For example, there is VBA code that will tell Internet Explorer to open a web site. This is an asynchronous process. That is, it just sends a request to IE then executes the next line of code. Meantime, IE is doing its thing. If VBA needs to know when IE is finished, it has to keep checking, because IE is a separate process outside Excel. That why I asked you if the process that opens your file is asynchronous. If you are simply using Open, then this exception does not apply to you.

    3. excel will not wait for a line of code to be finished executing before moving on to the next one

    Incorrect, as explained above.

    In your original post you talked about downloading a file from SAP, but in your later post with a code sample you show that you are simply opening a file, not downloading it from anywhere. So in your last post the Wait statement is useless.

    Like I said, I really need to see all of your code to help you with this.

  5. #5
    Registered User
    Join Date
    05-02-2018
    Location
    CT, USA
    MS-Off Ver
    2007/10
    Posts
    23

    Re: Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

    Thank you so much for the reply! - I understand these concepts now and was able to adjust accordingly!

    It seems that most of the SAP GUI scripting is definitely an atomic function, but the open function (from within SAP) seems to be an asynchronous process so I needed to stick the wait after that.

    I am truly grateful for your explanations. Thank you

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Am I using DoEvents incorrectly? or is there an alternative besides what I'm doing?

    Glad to help, hope you get it all working. I have never used SAP.

+ 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. VBA code stuck in doevents occasionally
    By JJchak in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2016, 09:23 PM
  2. Where to place DoEvents
    By demarc in forum Excel General
    Replies: 2
    Last Post: 04-11-2015, 04:12 AM
  3. Need some help with DoEvents
    By raghuramn.88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2013, 11:27 AM
  4. VBA DoEvents function 'magic'
    By danleonida in forum Tips and Tutorials
    Replies: 7
    Last Post: 02-18-2013, 01:34 PM
  5. How to utilize DoEvents in VBA?
    By Soomro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2009, 05:37 PM
  6. DoEvents not working
    By TheHawk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2008, 05:47 PM
  7. DoEvents??
    By Geoff in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-18-2005, 10:23 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