在 Excel 中刷新并另存为文本文件

Refreshing and Saving as a text file in Excel

我一直在尝试自动刷新我的 table(通过 power query 从 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
Application.Wait (Now + TimeValue("0:00:05"))
DoEvents