使用VBA

时间:2018-12-26 15:25:02

标签: excel vba excel-vba powerquery

我有一个VBA代码,可以刷新两次数据连接,然后计算工作簿。我添加了“错误恢复继续”,但是刷新查询时出现错误时,它将忽略我的VBA错误处理,并显示一条错误消息:“意外错误,出了点问题。如果问题继续,请重新启动Excel。”单击取消后,它将继续执行我的代码,但是导致错误的一个查询仍然显示其运行,即使其他查询在此之后运行。我已经关闭了所有连接的后台刷新功能,我只是想找到一些VBA代码,以便可以使用电源查询来处理错误。

最终,我希望我的代码取消导致错误的查询,并在完成所有代码后单独刷新它。

我们如何使用VBA处理功率查询中的错误?

当前代码:

On Error Resume Next 
ActiveWorkbook.RefreshAll 
ActiveWorkbook.RefreshAll 
ActiveWorkbook.Calculation = xlAutomatic

1 个答案:

答案 0 :(得分:1)

Within Power Query you can wrap you code as (assuming this is Query1):

try
  let
  ....

  in
  ...

otherwise
  "Error"

and then having one error wrapping query for all queries as:

if Query1 = "Error" then "Error" 
else if Query2 = "Error" then  "Error"
...
else "No Error"

which you can then load to Excel as a new sheet and check with VBA macro whether the value of such a cell contains "Error" or not.