View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
David David is offline
external usenet poster
 
Posts: 10
Default Error Handling - On Error GoTo doesn't trap error successfully

Jim - Thanks for the reply. I've put in the relevant section of code
below but let me try to describe the situation more clearly to spare
you the time to wade through the whole listing. I appreciate your time
and will be as concise as I can.

I have programmed a procedure to go to Yahoo's quote server and pull
down quotes. The quoteserver sends back a CSV file that I've parsed
into columns. This creates a table which has a row for each ticker and
a column for each data item about the ticker, e.g. Open, High, Low,
Close, Volume, CompanyName, and so on. This procedure works fine.

If Yahoo does not have a certain data item, then the quote server sends
down the string "N/A" This is simply a string, not to be confused with
the excel error code which can be detected with ISNA().

The next procedure I wrote (excerpted below) attempts to retrieve the
value from the page where Yahoo puts it and assign it to a variable.
The error happens in this line or ones like it:

cLow = RetrieveData(rRetrData, sTicker, "low")

where RetrieveData() is a function that I wrote which takes a range,
column name and row name and finds the data item sought. sTicker is a
string variable which is the row name and "Low" is the header for the
column.

The RetrieveData function goes to work and comes back with a string
"N/A" which of course does not fit into the variable cLow which is Dim
as Currency. This causes the Type Mismatch Error.

All I really need my procedure to do is to stop trying to process that
particular ticker, and skip to the next ticker.

Any suggestions as to better ways to make the program get past the
missing data would be highly appreciated. I know I could just Dim the
variables as variants instead of currency but that just makes the
problem resurface downstream in later processing when other procedures
try to do mathematical operations on the data.





Sub CalculateIntradayResults()

Dim cLast As Currency 'last price
Dim cHigh As Currency 'intraday high from Yahoo, from the current
refresh cycle
Dim cLow As Currency 'intraday Low from Yahoo from the current
refresh cycle
Dim cPriorHigh As Currency ' intraday high from the previous
refresh cycle
Dim cPriorlow As Currency 'intraday low from the previous refresh
cycle
Dim cPriorHighSinceLow 'highest price since intraday low in prev
refresh cycle
Dim cPriorLowSinceHigh ' Lowest price since intraday high in prev
refresh cycle

'step through the retrieved data one ticker at a time and extract
values to variables
For iRow = iFirstRow To iLastRow
sTicker = rIntraCalc.Cells(iRow, 1)
On Error GoTo MissingData
cLast = RetrieveData(rRetrData, sTicker, "Last")
On Error GoTo MissingData
cHigh = RetrieveData(rRetrData, sTicker, "High")
On Error GoTo MissingData
cLow = RetrieveData(rRetrData, sTicker, "low")
On Error GoTo MissingData
cPriorHigh = RetrieveData(rIntraCalc, sTicker, "IntraHigh")
On Error GoTo MissingData
cPriorlow = RetrieveData(rIntraCalc, sTicker, "IntraLow")
On Error GoTo MissingData
cPriorHighSinceLow = RetrieveData(rIntraCalc, sTicker,
"HiSinceLow")
On Error GoTo MissingData
cPriorLowSinceHigh = RetrieveData(rIntraCalc, sTicker,
"LowSinceHi")

'once all the data has been captured in variables, test some
conditions and record results
If cLast cPriorHigh Then
[skipped code not relevant to this problem]
End If

[ skipped code not relevant to this problem]

GoTo ResumeWithoutError: ' this skips the error message below
if no problems.
MissingData:
'MsgBox "missing data problem with " & sTicker & " in calculate
intraday results"
' this will fire once at the end of run because the last cell
is blank.
ResumeWithoutError:
Next iRow

End Sub