tags:

views:

20

answers:

1

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 the 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!

+1  A: 

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
belisarius