Delay VBA code operations without freezing Excel

﹥>﹥吖頭↗ 提交于 2021-01-29 02:10:15

问题


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

回答1:


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



回答2:


Check this out,

You may have to do some editing but, I believe you will get the idea.

Sub Button1_Click()
    Dim wb As Workbook, bk As Workbook, bk2 As Workbook

    Set wb = ThisWorkbook
    Set bk = Workbooks.Open("Q:\Quality Control\Internal Failure Log - Variable Month.xlsm")
    Set bk2 = Workbooks.Open("Q:\Reports\Finished-Transfer Report-variable month.xlsm")

        With bk
            .RefreshAll
            .Save
            .Close
        End With

        With bk2
            .RefreshAll
            .Save
            .Close
        End With

        With wb
            .RefreshAll
            .Save
            .Close
        End With

    End Sub



回答3:


Try using Application.Wait Now+TimeSerial(<hours>,<minutes>,<seconds>). Works for me whenever I need to have a delay in the code execution.



来源:https://stackoverflow.com/questions/29569217/delay-vba-code-operations-without-freezing-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!