+ Reply to Thread
Results 1 to 10 of 10

time delay in a macro

Hybrid View

thesnake505 time delay in a macro 01-21-2013, 06:24 PM
Sean Thomas Re: time delay in a macro 01-21-2013, 06:43 PM
thesnake505 Re: time delay in a macro 01-22-2013, 08:29 AM
noclass1980 Re: time delay in a macro 01-22-2013, 08:43 AM
thesnake505 Re: time delay in a macro 01-22-2013, 11:11 AM
noclass1980 Re: time delay in a macro 01-22-2013, 11:56 AM
thesnake505 Re: time delay in a macro 01-22-2013, 04:40 PM
noclass1980 Re: time delay in a macro 01-22-2013, 06:16 PM
thesnake505 Re: time delay in a macro 01-22-2013, 06:54 PM
HitTheEXCELerator Re: time delay in a macro 01-25-2013, 07:43 PM
  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    14

    time delay in a macro

    I have a macro that runs an online report, the report has an option to export the report to excell. My macro tells the report to do so, but then I have a 2nd macro to split the data up. However it takes time to down load the online report so I need a pause. Application.wait doesnt work because it stops the download process. So I need something else! Any ideas?

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: time delay in a macro

    have yo tried the sleep function?

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=626
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Registered User
    Join Date
    01-07-2013
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: time delay in a macro

    hi, i have tried this but i cant get it to work!

  4. #4
    Registered User
    Join Date
    09-27-2012
    Location
    Warrington, England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: time delay in a macro

    Hi, I'v had this problem as well and solved it by adding a do until loop in until sufficient time has passed to allow the other actions to complete. Insert the following lines immediately after you've started the loading process where you need to pause.

    Sec1 = Now + TimeValue("00:00:04")
    Do Until Now > Sec1
    'DoEvents
    Loop
    the above code waits 4 seconds before continuing. You might need to adjust the wait time to suit your needs. Might be a bit crude but it works for me. Depending on what the code is doing you may or may not need the DoEvents line (hence the ').
    Last edited by noclass1980; 01-22-2013 at 08:44 AM. Reason: remove typos

  5. #5
    Registered User
    Join Date
    01-07-2013
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: time delay in a macro

    this doesnt seam to work either, it still stalls the downlaod process.

    heres that part of my code

    "
    While ie.busy
    DoEvents
    Wend
    Application.Wait Now + TimeSerial(0, 0, 15)
    SendKeys "O", True
    sec1 = Now + TimeValue("00:00:20")
    Do Until Now > sec1
    DoEvents
    Loop


    Windows("PPW.EF[1].xls").Activate"


    now, i get subscrit out of range for my last command. this is because "sendkeys "O", true" is the bit that tells ie to save or install the file. but the next part paues this download process and the next command cant run!

  6. #6
    Registered User
    Join Date
    09-27-2012
    Location
    Warrington, England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: time delay in a macro

    ok, one question: why is there the 20 second delay after the Sendkeys?

    I have two suggestions to try:

    this is some code I wrote to open a new ie page (navigating to Google), waiting until ie ready, then pasting some previously copied text ("FText") from the ClipBoard.

    Set newpage = New InternetExplorer
    
        newpage.Visible = True
        newpage.Navigate ("www.translate.google.com")
        Do
    
    Loop Until newpage.ReadyState = READYSTATE_COMPLETE
    MyData.GetFromClipboard
    FText = MyData.GetText
    Sec1 = Now + TimeValue("00:00:04")
    Do Until Now > Sec1
    DoEvents
    Loop
    
    SendKeys ("^v"), True
    maybe you can substitute the Google line with your web address for the file. The programme pauses until ie is ready but then i found that I had to introduce the 4 second delay before pasting in the Clipboard using the Sendkeys CTRL V but you'll need to change this to "O" (see comment on Sendkeys below).
    Also make sure that you have the right References checked ("Tools", "References") as I think you might need the Microsoft Internet Controls ref checked. (Might be wrong though...)

    Alternatively, you could put the sendkeys and Windows.Activate command in a separate sub (YourSubName) and use the following Code to call the sub to run after a period of time (in this case 20 seconds) and then sub continues with the rest. if the time is sufficient, this should allow ie to catch up and be ready for the sendkeys commands that you sent earlier.

    DDuration = Now + TimeValue("00:00:20")
    Application.OnTime DDuration, "YourSubName"
    your ie stuff here
    To see how the Ontime works, paste the following into a new module of a new book and then run the "testontime" macro to see what happens.

    Public Sub testontime()
    
    waittime = Now + TimeValue("00:00:05")
    Application.OnTime waittime, "NextMsg"
    MsgBox "ha!"
    End Sub
    
    
    Public Sub NextMsg()
    MsgBox "Next MsG"
    End Sub

    Lastly, using Sendkeys can be temperamental and I've found that using/not using brackets (as in my example) can make a difference. Just have to try the different approaches I'm afraid.
    Last edited by noclass1980; 01-22-2013 at 11:57 AM. Reason: typos (again)

  7. #7
    Registered User
    Join Date
    01-07-2013
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: time delay in a macro

    The reason for the delay is because ie is converting the report to a spresdsheet, however I have just clicked, ie may not be doing it, I may need to make excel and object and have a
    While excel.busy
    Do event
    Wend
    Windows ("ppw.ef1.xls").activate
    May be my problem

  8. #8
    Registered User
    Join Date
    09-27-2012
    Location
    Warrington, England
    MS-Off Ver
    Excel 2007
    Posts
    27

    Re: time delay in a macro

    sorry, don't really understand what you are trying to do. good luck

  9. #9
    Registered User
    Join Date
    01-07-2013
    Location
    england
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: time delay in a macro

    Ok, we have s reportin systrm at work, my macro takes control, opens the page, inserts yesterdays date, views the report, then there is an option to export the report to excel. However the command after the export always fails because the report isnt exported. All the wait commands pause excel which also pause the download process. Which means after the pause there still isnt a report. Does this make more sence

  10. #10
    Registered User
    Join Date
    08-23-2011
    Location
    Toronto,Ontario
    MS-Off Ver
    Excel 2003
    Posts
    48

    Re: time delay in a macro

    This is the simple timer code I use.

    Sorry I can't remember the proper format for written code.

    TimeDelay = Timer
    StopTimer = TimeDelay + x(Time in seconds)
    While TimeDelay < StopTimer
    DoEvents
    TimeDelay = Timer
    Wend

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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