0

I am making a macro that I want to do the following things:

  1. Open a file.

  2. Wait till the file fills with data (this file uses cell formulae to download data from external database, the download takes approx 15 seconds)

  3. After 20 seconds check if everything is downloaded, if no, wait further 10 seconds (up to minute total waiting).
  4. If everything is downloaded, archive and close the file.

The problem I have is stopping code execution for some time while letting the formulae update themselves in the meantime - I tried Application.Wait after reading at MSDN that:

The Wait method suspends all Microsoft Excel activity and may prevent you from performing other operations on your computer while Wait is in effect. However, background processes such as printing and recalculation continue."

but it didn't work - Wait stopped both the code and the data download. Is there any way to stop the code for a while but let all other Excel activity continue? I'd like the macro to be able to run completely unattended as it is possible that it will have to be ran in the middle of the night.

Edit: tried Siddarth's answer and it doesn't work for me. The code I tried:

Sub processfile()

    Dim bbgwb As Workbook
    Dim filepath As String

    filepath = "C:\Test\0900CET.xls"
    Set bbgwb = Workbooks.Open(filepath)

    Wait 60

    filepath = "C:\Test\Archive\"
    If Len(Dir(filepath)) = 0 Then
        MkDir filepath
    Else
        'Do nothing.
    End If

    filepath = "C:\Test\Archive\0900.xls"

    bbgwb.Worksheets(1).Range("A1:AZ200").Copy
    bbgwb.Worksheets(1).Range("A1:AZ200").PasteSpecial xlPasteValuesAndNumberFormats
    bbgwb.Worksheets(1).Range("C142").Value = Now

    bbgwb.SaveAs Filename:=filepath, FileFormat:=56
    bbgwb.Close
    ThisWorkbook.Close

End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

0900CET.xls has the formulae which, after opening it, autoupdate with data (which I want then store and archive, that's why I add the whole copy/paste special part at the end). I just tested opening 0900CET.xls manually (all formulae updated after 17.3 seconds) and via the code above (not a single cell updated before the file got archived.

1
  • to wait few sec without suspending Excel try this solution Commented Jun 19, 2015 at 11:28

1 Answer 1

1

I usually use this sub that I created for my self

Sub Sample()
    '
    '~~> Do Something
    '
    Wait 5 '<~~ Wait for 5 seconds. Change as applicable
    '
    '~~> Do Something
    '
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Consider using Now since Timer can fool you if you cross midnight.
Unfortunately this answer doesn't work for me, I edited the question to add results of testing, including the code I have at the moment.
That is because you are opening the file in the same Excel instance. So till the time the file is not opened, the rest of the code will not run.
I think I don't understand what you say here. Just to try to make it more clear: all of my code executes as expected, but the Wait sub doesn't make the formulae update as I want them to - it seems it takes up enough system resources so that 60 seconds wait is not enough when, if opened manually, the file takes less than 20 seconds to update itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.