提问人:T-Rex 提问时间:11/14/2023 更新时间:11/14/2023 访问量:35
用于刷新 Power Query 的 VBA 不允许有足够的时间在保存和关闭时刷新查询
VBA to Refresh Power Query does not allow enough time to Refresh Queries upon save and close
问:
我们的目录中有 185 个文件需要更新。有两个 Power Query,在使用以下代码时,它们都不会刷新。另外值得一提的是,如果我打开每个单独的文件,第一个查询会更新,但第二个查询不会。我将不得不全部刷新。这些文件是高度机密的,我不能让其中一个文件不更新,从而将记录发布给另一个销售人员。这些文件应该在明天 11 年 15 月 23 日发出,我被卡住了。请帮忙!我也尝试过刷新。
Sub UpdatePowerQuery()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim WB As Workbook
Dim sourceBook 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
Application.DisplayAlerts = False
'Bypasses the Update Links
Application.AskToUpdateLinks = False
MsgBox "WARNING: Make sure you pick the correct folder!"
'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)
'Code between lines is performed on each workbook in the folder
'************************************************************************
WB.Queries.FastCombine = True 'ignores privacy levels on all computers
WB.Connections("Query - SF Data").Refresh
WB.Connections("Query - SF Data Totals").Refresh
Application.Wait (Now + TimeValue("00:00:15"))
'************************************************************************
'Save and Close Workbook
WB.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
'sourceBook.Close
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
End Sub
答:
3赞
Sam Nseir
11/14/2023
#1
是否启用了“后台刷新”?如果是这样,请尝试禁用它。https://www.excelcampus.com/vba/enable-background-refresh-on-all-power-query-connections/
评论