延迟 VBA 代码操作而不冻结 Excel
Delay VBA code operations without freezing Excel
我正在编写一个 VBA 宏,它将简单地打开另一个工作簿,刷新所有内容,保存,然后关闭。它将用第二个工作簿重复此操作。然后刷新并保存 VBA 代码来自 运行 的主工作簿。我面临的问题是,正在打开的两个工作簿正在使用 Microsoft 查询从我们的 JobBoss 数据库中提取数据,并且设置为在打开文件时刷新此数据。因此文件打开,开始刷新查询数据,然后 VBA 代码尝试同时刷新导致错误。此外,它不会等待数据完成刷新后再尝试保存和关闭它。
有没有办法在不冻结 Excel 应用程序的情况下延迟每个操作?
我试过 application.wait
但 Excel 在此期间被冻结并且数据不会在后台刷新,因为一旦时间结束 Excel 解冻,我仍然收到关于在刷新过程中尝试执行操作的错误。
这是我目前拥有的:
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
看看这个,
您可能需要做一些编辑,但我相信您会明白的。
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
尝试使用 Application.Wait Now+TimeSerial(<hours>,<minutes>,<seconds>)
。每当我需要延迟代码执行时为我工作。
此代码在进入并禁用我所有查询表的后台刷新后有效。
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
我正在编写一个 VBA 宏,它将简单地打开另一个工作簿,刷新所有内容,保存,然后关闭。它将用第二个工作簿重复此操作。然后刷新并保存 VBA 代码来自 运行 的主工作簿。我面临的问题是,正在打开的两个工作簿正在使用 Microsoft 查询从我们的 JobBoss 数据库中提取数据,并且设置为在打开文件时刷新此数据。因此文件打开,开始刷新查询数据,然后 VBA 代码尝试同时刷新导致错误。此外,它不会等待数据完成刷新后再尝试保存和关闭它。
有没有办法在不冻结 Excel 应用程序的情况下延迟每个操作?
我试过 application.wait
但 Excel 在此期间被冻结并且数据不会在后台刷新,因为一旦时间结束 Excel 解冻,我仍然收到关于在刷新过程中尝试执行操作的错误。
这是我目前拥有的:
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
看看这个,
您可能需要做一些编辑,但我相信您会明白的。
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
尝试使用 Application.Wait Now+TimeSerial(<hours>,<minutes>,<seconds>)
。每当我需要延迟代码执行时为我工作。
此代码在进入并禁用我所有查询表的后台刷新后有效。
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