vbaexcelwait

Delay VBA code operations without freezing Excel


I am writing a VBA macro that will simply open another workbook, refresh all, save, and then close. It will repeat this with a second workbook. Then it refreshes and save the main workbook that the VBA code was run from. The problem I am facing is that the two workbooks being opened are using Microsoft queries to pull data from our JobBoss database and it is set to refresh this data upon opening the file. So the file opens, starts refreshing the query data and then the VBA code tries to refresh at the same time causing an error. Also, it doesn't wait for the data to finish refreshing before trying to save and close it.

Is there a way to delay each operation without freezing the Excel application?

I've tried application.wait but Excel is frozen during this time and the data is not refreshing in the background as once the time is over Excel unfreezes and I still receive the error about trying to do an operation while a refresh is in progress.

This is what I have so far:

Sub Refresh_All()
'
' Refresh_All Macro
'
' Keyboard Shortcut: Ctrl+r
'
    ChDir "Q:\Quality Control"
    Workbooks.Open Filename:= _
        "Q:\Quality Control\Internal Failure Log - Variable Month.xlsm"
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.Save
    ActiveWindow.Close
    ChDir "Q:\Reports"
    Workbooks.Open Filename:= _
        "Q:\Reports\Finished-Transfer Report-variable month.xlsm"
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.Save
    ActiveWindow.Close
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.Save
End Sub

Solution

  • This code works after going in and disabling background refresh on all of my query tables.

    Sub Refresh_All()
    '
    ' Refresh_All Macro
    '
    ' Keyboard Shortcut: Ctrl+Y
    '
        ChDir "Q:\Quality Control"
        Workbooks.Open Filename:= _
            "Q:\Quality Control\Internal Failure Log - Variable Month.xlsm"
        Dim endTime As Date
        endTime = DateAdd("s", 2, Now())
        Do While Now() < endTime
    DoEvents
        Loop
        ActiveWorkbook.RefreshAll
           endTime = DateAdd("s", 10, Now())
        Do While Now() < endTime
            DoEvents
        Loop
    ActiveWorkbook.Save
        endTime = DateAdd("s", 5, Now())
        Do While Now() < endTime
            DoEvents
        Loop
        ActiveWindow.Close
        ChDir "Q:\Reports"
        Workbooks.Open Filename:= _
            "Q:\Reports\Finished-Transfer Report-variable month.xlsm"
            endTime = DateAdd("s", 2, Now())
        Do While Now() < endTime
            DoEvents
        Loop
    ActiveWorkbook.RefreshAll
         endTime = DateAdd("s", 10, Now())
        Do While Now() < endTime
            DoEvents
        Loop
    ActiveWorkbook.Save
            endTime = DateAdd("s", 5, Now())
        Do While Now() < endTime
            DoEvents
        Loop
    ActiveWindow.Close
        ActiveWorkbook.RefreshAll
            endTime = DateAdd("s", 10, Now())
        Do While Now() < endTime
            DoEvents
        Loop
    ActiveWorkbook.Save
    End Sub