我正在尝试自动执行 Excel 查询刷新报告,并且 Excel 不断循环遍历 Application.CalculateUntilAsyncQueriesDone。
我的目标是让VBA打开Excel报告,刷新它,然后保存它。
Office 版本:- Microsoft® Excel® for Microsoft 365 MSO(版本 2302 内部版本 16.0.16130.20894)64 位。
这是 MSO 365 上的已知错误吗?
Sub LoopAllExcelFilesInFolder()
'To loop through all Excel files in a user specified folder and refresh power query connections
Dim wb As Workbook
Dim myPath As String
Dim MyFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
MyFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While MyFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & MyFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Refresh all connections and calculate until async queries are done
'Wait for Refresh to finish before running vba
wb.RefreshAll
With Application
Application.Calculation = xlCalculationAutomatic
Application.CalculateUntilAsyncQueriesDone
Do Until .CalculationState = xlDone
Loop
Application.Calculation = xlCalculationManual
End With
If Not Application.CalculationState = xlDone Then
DoEvents
End If
'Save and Close Workbook
wb.Close savechanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
MyFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
立即编码,完全正常工作,并作为刷新多个设备的绝佳工具。
该功能还可以处理同步的 Onedrive 文件。
Sub LoopAllExcelFilesInFolder()
'To loop through all Excel files in a user specified folder and refresh power query connections
Dim wb As Workbook
Dim myPath As String
Dim MyFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
MyFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While MyFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & MyFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Refresh all connections and calculate until async queries are done
'Disable background refresh for all connections
For Each conn In wb.Connections
conn.OLEDBConnection.BackgroundQuery = False
Next conn
wb.RefreshAll
'Wait for Refresh to finish before running vba
With Application
Application.Calculation = xlCalculationAutomatic
Application.CalculateUntilAsyncQueriesDone
Do Until .CalculationState = xlDone
Loop
Application.Calculation = xlCalculationManual
End With
If Not Application.CalculationState = xlDone Then
DoEvents
End If
'Re-enable background refresh for all connections
For Each conn In wb.Connections
conn.OLEDBConnection.BackgroundQuery = True
Next conn
'Save and Close Workbook
wb.Close savechanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
MyFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Refresh Completed!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub