I've written a simple VBA program to download stock quotes from a portfolio in Google Finance. It works fine for a couple of hours and then hangs up. In the applications status bar it says "Connecting to" (internet?). Once stuck it won't respond to the ESC key and I have force it to end with the Windows Task Manager.
The portfolio is accessed once every 5 minutes and the data placed at A1 is copied to a separate page for storage. The code to access the portfolio is:
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://www.google.com/finance#", Destination:=Range("$A$1"))
.Name = "finance#"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """portfolio1"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
The error occurs randomly, usually after a long period of time (hours) and it doesn't appear to be time of day dependent.
I tried setting Refresh BackgroundQuery:=TRUE with the result being a message box pops up when the program hangs. Acknowledging the message box appears to clear the problem but I need the program to run autonomously and handle these hiccups without a babysitter.
I found this problem results from using Application.wait to create a timer but can't say what the mechanics of the stalling are, just that the execution stops at the query command. My original program had a timer which counted down 5 minutes and then queried Google for current stock prices for ticker symbols in a Google portfolio. The solution has been to use Application.OnTime instead. A side benefit of this is Excel's attention is fully consumed with Application.wait such that nothing can be done in Excel while its running. Application.OnTime on the other hand seems to offload the timer function to hardware, or ??, such that Excel itself is available to do other things while waiting for the timer to time out.
The whole program looks like this:
The Sub TicToc creates a countdown timer display indicating how long until the next quote. A 'RUN' button points to this macro to begin the program. When the program is first opened all variables are zero the macro will set the timer display to "00:00" and call the Quote macro, then re-initializes the count down timer and starts the timer macro. A stop macro is also included. After STOPping if RUN is pressed again the timer will pick up where it left off unless the clock has been manually reset (Reset_Clock macro and a user button).
Its probably good form to delete connections and query tables when you're done with them. In debugging the first program I accumulated over 800 connections so I added a couple of loops to clean these up. This now occurs at the end of the Quote macro.