2016-09-21 34 views
0

我想刷新我的工作簿,然后在单元上运行检查,如果值> 0显示一条消息,则代码我看起来正确且逻辑性强,但刷新是在检查值之后完成的,我试图把它们分成单独的宏并按顺序调用它们,但刷新后仍然运行。不确定是否值得注意刷新涉及刷新到SQL DB的数据连接。Excell RefreshAll在其他宏之后运行

这是两个宏我都不得不时刻:

Sub RefreshMacro() 

ActiveWorkbook.RefreshAll 
Sheets("Execution").Select 
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh 
Sheets("Traffic Lights").Select 
ActiveWorkbook.RefreshAll 

End Sub 
Sub ErrorMessage() 
If Sheets("Traffic Lights").Range("G2").Value > "0" Then 
MsgBox "Error with data!" & vbCr & 
"Please Note There is an issue with the data" & vbCr & 
"See Traffic Lights for more details!", vbOKOnly + vbExclamation, 
"Red Traffic Lights" 
End If 
End Sub 
+0

当你一步通过它它工作? –

+0

是的,如果我刹车后刷新它的工作 –

+0

看看http://stackoverflow.com/questions/22083668/wait-until-activeworkbook-refreshall-finishes-vba –

回答

0

行,所以我曾尝试各种不同的人得到这个工作,并与下面的管理, 这似乎这样的伎俩:

Application.CalculateUntilAsyncQueriesDone 
Application.CalculateFullRebuild 
Application.CalculateUntilAsyncQueriesDone 

如在下面的完整的查询

Sub CheckTrafficLights2() 
    For Each objConnection In ThisWorkbook.Connections 
      objConnection.Refresh 
DoEvents 
    Next 
ThisWorkbook.RefreshAll 

Application.CalculateUntilAsyncQueriesDone 
Application.CalculateFullRebuild 
Application.CalculateUntilAsyncQueriesDone 

Sheets("Execution").PivotTables("PivotTable1").PivotCache.Refresh 
    If Sheets("Traffic Lights").Range("G2").Value > "0" Then 
     MsgBox "Error with data!" & vbCr & "Please Note There is an issue with the data" & vbCr & "See Traffic Lights for more details!", vbOKOnly + vbExclamation, "Red Traffic Lights" 
    End If 
End Sub 
0

下面是在此基础上link

首先解决了两个解决方案:

Sub CheckTrafficLights1() 


    ActiveWorkbook.RefreshAll 
    DoEvents 
    Sheets("Execution").PivotTables("PivotTable1").PivotCache.Refresh 
    DoEvents ' Not sure if necessary. 

    If Sheets("Traffic Lights").Range("G2").Value > "0" Then 
     MsgBox "Error with data!" & vbCr & 
     "Please Note There is an issue with the data" & vbCr & 
     "See Traffic Lights for more details!", vbOKOnly + vbExclamation, 
     "Red Traffic Lights" 
    End If 
End Sub 

解决方法二:

Sub CheckTrafficLights2() 

    For Each objConnection In ThisWorkbook.Connections 
     'Get current background-refresh value 
     bBackground = objConnection.OLEDBConnection.BackgroundQuery 

     'Temporarily disable background-refresh 
     objConnection.OLEDBConnection.BackgroundQuery = False 

     'Refresh this connection 
     objConnection.Refresh 

     'Set background-refresh value back to original value 
     objConnection.OLEDBConnection.BackgroundQuery = bBackground 
    Next 
    Sheets("Execution").PivotTables("PivotTable1").PivotCache.Refresh 
    DoEvents ' Not sure if necessary. 

    If Sheets("Traffic Lights").Range("G2").Value > "0" Then 
     MsgBox "Error with data!" & vbCr & 
     "Please Note There is an issue with the data" & vbCr & 
     "See Traffic Lights for more details!", vbOKOnly + vbExclamation, 
     "Red Traffic Lights" 
    End If 
End Sub 
+0

第一个解决方案无法在显示错误消息之前更新数据连接 –

+0

第二个解决方案在第二个解决方案上生成应用程序定义的或对象定义的错误(运行时错误1004) –

+0

我已将连接更改为ODBCConnection,因为这是我正在使用,仍然失败,如上 –

相关问题