【发布时间】:2015-04-08 14:54:45
【问题描述】:
我一直在尝试自动刷新我的表(通过电源查询从 SQL 数据库获得)并将其直接保存为文本文件(制表符分隔) 我对 VBA 很陌生,我使用的宏是
Public Sub UpdatePowerQueries()
'Macro to update the Power Query script(s) and save the file as .txt
Dim cn As WorkbookConnection
For Each cn In ThisWorkbook.Connections
If Left(cn, 13) = "Power Query -" Then cn.Refresh
Next cn
Application.DisplayAlerts = False
ActiveSheet.SaveAs Filename:="customfile" & Format(Date, "yyyymmdd") & ".txt", FileFormat:=xlTextWindows
Application.DisplayAlerts = True
End Sub
现在我一直面临的问题是刷新部分和保存部分各自工作正常,但如果我将它们放在同一个宏中,保存部分发生得太快,文本文件为空。谁能帮帮我?
谢谢
【问题讨论】:
-
您有几个不同的选择,尽管
DoEvents应该可以。看这个帖子讨论Wait until ActiveWorkbook.RefreshAll finishes - VB -
非常感谢!一旦我禁用了查询的后台刷新,DoEvent 就起作用了。
标签: vba excel powerquery