我想刷新我的工作簿,然后对单元格进行检查,如果值为> 0显示一条消息,我的代码似乎正确且符合逻辑,但刷新是在检查值之后完成的,我已经尝试将它们分成单独的宏并按顺序调用它们,但刷新仍然在检查后运行。不确定是否值得注意刷新涉及刷新与SQL DB的数据连接。
这是我目前拥有的两个宏:
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)
以下是基于此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
答案 1 :(得分: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