2016-09-21 59 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,因爲這是我正在使用,仍然失敗,如上 –

相關問題