
1067 Information from the Web
41.8.4 Get Prices: A Third Approximation
One improvement our application could defi nitely do with is some
control over the data retrieved. The third approximation gives us control
of some of the relevant parameters for the Yahoo Web site from the user.
It makes use of the user interaction described in Chapter 38. The macro
gets the date of the last sample to be collected from the user. In order
to keep the macro readable and of a manageable size, the actual inter-
action with the user will take place in a function. Notice that six of the
parameters to the Yahoo Web site are affected by the end date and will
be replaced by the results of the function.
The difference between GetPricesVer3 and version 2 is the replace-
ment of two of the parameter strings by the function GetDates( ); the
function call is set in bold type on a shaded background for clarity:
Sub GetPricesVer3()
Dim Ws As Worksheet ‘Our query is going to be
‘stored here
Dim Wq As QueryTable ‘This will be our query
Dim Url As String ‘Full Web address on Yahoo
Url = _
“URL;” & _
“http://ichart.fi nance.yahoo.com/table.csv?” & _
“s=QQQQ&” & _
GetDates() & _
“g=d&” & _
“ignore=.csv”
‘Use ActiveSheet for data; store reference in ws
Set Ws = ActiveSheet
‘Add our query to the worksheet;
‘results should go to column A
‘down from A2; store reference in wq
Set Wq = Ws.QueryTables.Add(Connection:=Url, _
Destination:=Ws.Range(“A2”))
‘Run our query in the foreground,
‘i.e., wait for the query to fi nish
Wq.Refresh BackgroundQuery:=False