I've built a QuoteServer.XLAM which includes numerous functions to fetch stock market data. I incorporate this in other projects which require stock market data. I am having a problem with the scope of an array declared in the QuoteServer.
One module of the QuoteServer includes global variables and utilities including a declaration for Public array varrTickerQuotes.
Option Compare Text
Option Explicit
Option Base 1 ' sets lower bound of array to 1
Public varrTickerQuotes() As Variant
Another module of the QuoteServer includes a Public Sub which loads the array upon request. This works perfectly in module test mode and when called from another project:
Public Sub QS_BuildTickerArray(varrSymbols As Variant)
.....
redim varrTickerQuotes to accommodate requested data
fill varrTickerQuotes with data
.....
End Sub
Another module of the QuoteServer includes a Public Function which extracts data from the array and returns it. This works perfectly in module test mode but does not work when called from another project:
Public Function QS_GetTickerQuote(pTicker As String, _
pFields As String) As Variant()
Dim vQuote() As Variant
Dim xField As Long, xRow As Long
ReDim vQuote(1, Len(pFields))
xField = 1
For xRow = 1 To UBound(varrTickerQuotes, 1)
If pTicker = varrTickerQuotes(xRow, 1) Then
If InStr(1, pFields, "D") Then
vQuote(1, xField) = varrTickerQuotes(xRow, 3)
xField = xField + 1
End If
If InStr(1, pFields, "P") Then
vQuote(1, xField) = varrTickerQuotes(xRow, 2)
xField = xField + 1
End If
If InStr(1, pFields, "V") Then
vQuote(1, xField) = varrTickerQuotes(xRow, 4)
xField = xField + 1
End If
If InStr(1, pFields, "A") Then
vQuote(1, xField) = varrTickerQuotes(xRow, 5)
xField = xField + 1
End If
Exit For
End If
Next
QS_GetTickerQuote = vQuote
End Function
I am working on a project in a separate XLSM which includes QuoteServer.XLAM. I am referencing both routines in the QuoteServer:
1) From project: I call QS_BuildTickerArray with a symbol list for which to collect data and fill the array. The array is properly filled as shown by debug.print statements.
2) From project: later, I ask Function QS_GetTickerQuote to return selected data from the array. This throws an error.
The debugger shows that the error is thrown because the subscript is out of range when attempting to reference varrTickerQuotes in Function QS_GetTickerQuote. This suggests to me that array varrTickerQuotes is somehow out of scope or its contents are being lost upon return to the caller following the first call to build the array.
Is there a way to preserve the contents of the loaded array when control returns to the caller? Or must I return the array to the caller and then pass it back to QuoteServer in the request to extract data?
Thank you.
Earl
Bookmarks