开发者

Excel: VBA and refreshall ended message?

This is Excel 2003. I'd like to know how long it takes an external query to complete and then update a cell in my spreadsheet with that ET. I have the following, but it doesn't work because the ET is only as long as it takes to initiate th开发者_如何学运维e refresh:

Sub Refresh()
    Dim StartTime, EndTime, ET

    StartTime = Timer
    ActiveWorkbook.RefreshAll
    EndTime = Timer
    ET = Format(EndTime - StartTime, "Fixed")
    Range("H27").Value = ET
    MsgBox (ET)
End Sub

So the ET is about 1 second, even though the data fetch takes a good 10 minutes.

The easy way out is to set background refresh to false, but this blocks the whole application and makes life miserable for a long time.

Is there some kind of signal or exception that I can catch in VBA that indicates "oh, a background refresh is done; now you can stop your timer and calculate the ET"?

Thanks!


I guess you need to use the AfterRefresh event.
Here is a forum discussion with a happy ending and examples.

Pasting the example from the referred page, just for link independence (you should add your timer storage and arithmetic):

This code goes on a Module:

Dim X As New Class1   
Sub Initialize_It()   
  Application.DisplayAlerts = False 
  Application.ScreenUpdating = True  
  diropen = "C:\Desktop\" 

  Workbooks.Open diropen & "Test.xls" , UpdateLinks:=0 

  Set X.qt = Workbooks("Test.xls").Sheets("Sheet1").QueryTables(1)   
  ActiveWorkbook.RefreshAll 

End Sub

This code goes on a Class Module:

Public WithEvents qt As QueryTable 
Private Sub qt_AfterRefresh(ByVal Success As Boolean) 
' Declare variables. 
Dim a As Integer 
Dim My_Prompt As String 

' Initialize prompt text for message box. 
My_Prompt = "Data refreshed or canceled."   

' Displays message box before refresh (or cancel) occurs. 
MsgBox My_Prompt 

ActiveWorkbook.Save 
Workbooks("Test.xls").Close 

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜