Nick,
Thanks for the help. I'm not an Excel VBA wiz by any means, and I'm
just now starting to get into IE automation and VBA/Internet
interactions. I played around with the y and z parameters in the URL
to try and understand how the website interprets the numbers.
I am interested though in learning more about web queries and as you
put it "talking to the server." If you know of any literature or can
point me in the right direction I'd greatly appreciate it.
The code that I wrote using your suggestions is below. I'm sure
there's a better way to do this, but it's working for now.
Tough the query gives me what I'm looking for, it takes longer than
navigating IE, telling the CSV file to Open, and then performing a
copy/paste. Additionally, the query brings the data in with items that
I'm not concerned about (i.e. deleting the - * Close price adjusted for
dividends and splits, Date...Open...High..., Splits, and Dividends
rows); however, this is an easy fix because I can write another small
macro to delete the unnecessary data.
In this scenario, the CSV file lumps all the data points together and
does not include splits or dividend lines. My main concern for my
analysis is the date and adjusted close columns.
Anyhow, I included the revised code below so that you can see what I
did. Again, I appreciate the help.
Thanks,
Matt
Sub Macro1()
Dim a 'start month
Dim b 'start day
Dim c 'start year
Dim d 'end month
Dim e 'end day
Dim f 'end year
Dim y
Dim z
Dim tic 'ticker
Dim mon 'month
Dim dy 'day
Dim yr 'year
Dim QT As QueryTable
Dim move
Dim outputCounter
Sheets(1).Select
tic = UCase(InputBox("Enter the desired ticker for 10 years of
historical prices.", "Ticker", "APOL"))
mon = Format(DateSerial(Year(Date), Month(Date), Day(Date)), "mmm")
dy = Format(DateSerial(Year(Date), Month(Date), Day(Date)), "dd")
yr = Format(DateSerial(Year(Date), Month(Date), Day(Date)), "yyyy")
'make sure single digit dates are 0x and not just x (e.g. 03 <> 3)
a = monthToNumber(mon)
b = dy
c = yr - 10
d = a
e = b
f = yr
y = 0
z = 0
'Set ie = CreateObject("InternetExplorer.Application")
' ie.Visible = True
' ie.navigate "http://ichart.finance.yahoo.com/table.csv?s=" & tic &
"&a=" & a & "&b=" & b & "&c=" & c & "&d=" & d & "&e=" & e & "&f=" & f &
"&g=d&ignore=.csv"
'End Sub
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/hp?s=" & tic & "&a=" & a &
"&b=" & b & "&c=" & c & "&d=" & d & "&e=" & e & "&f=" & f & "&g=d&z=" &
z & "&y=" & y _
, Destination:=Range("A1"))
'.Name = "hp?s=LLTC&a=00&b=24&c=1997&d=00&e=24&f=2007&g=d"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xl
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "20"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Set QT = Worksheets(1).QueryTables(1)
'copy current data
Worksheets(1).Select
Range("a1").CurrentRegion.Copy
Worksheets(2).Select
outputCounter = Range("a1").CurrentRegion.Rows.Count
Range("a" & outputCounter + 1).PasteSpecial Paste:=xlPasteValues
'cycle back in time
For move = 66 To 3130 Step 66
Worksheets(1).Select
With QT
Debug.Print .Connection
.Connection = Replace(.Connection, "z=" & move - 66, "z=" & move)
.Connection = Replace(.Connection, "y=" & move - 66, "y=" & move)
'.BackgroundQuery = True
.Refresh False
End With
If Range("a2").Value = "" Then 'isempty(range("a2").value)
Exit For
Else
Range("a1").CurrentRegion.Copy
Worksheets(2).Select
outputCounter = Range("a1").CurrentRegion.Rows.Count
Range("a" & outputCounter + 1).PasteSpecial Paste:=xlPasteValues
End If
Next
Worksheets(2).Range("a1").Select
End Sub
Private Function monthToNumber(ByVal mon)
Select Case mon
Case "Jan"
monthToNumber = Right(100, 2)
Case "Feb"
monthToNumber = Right(101, 2)
Case "Mar"
monthToNumber = Right(102, 2)
Case "Apr"
monthToNumber = Right(103, 2)
Case "May"
monthToNumber = Right(104, 2)
Case "Jun"
monthToNumber = Right(105, 2)
Case "Jul"
monthToNumber = Right(106, 2)
Case "Aug"
monthToNumber = Right(107, 2)
Case "Sep"
monthToNumber = Right(108, 2)
Case "Oct"
monthToNumber = Right(109, 2)
Case "Nov"
monthToNumber = Right(110, 2)
Case "Dec"
monthToNumber = Right(111, 2)
End Select
End Function
> What about using a web query ?
>
[quoted text clipped - 137 lines]
>
> > End Function- Hide quoted text -- Show quoted text -
NickHK - 25 Jan 2007 10:21 GMT
Matt,
I'm no web expert, but may be check for using "HTTP request".
NickHK
> Nick,
>
[quoted text clipped - 174 lines]
> >
> > Looking at the URL that actual sent for a particular date range:http://finance.yahoo.com/q/hp?s=LLTC&a=02&b=26&c=1990&d=00&e=24&f=200.
..
> > and allowing for the extra pages with the z and y parametershttp://finance.yahoo.com/q/hp?s=LLTC&a=02&b=26&c=1990&d=00&e=24&f=
200...
> > So you can:
> > - Amend the web query's Connection string
[quoted text clipped - 131 lines]
> >
> > > End Function- Hide quoted text -- Show quoted text -
NickHK - 26 Jan 2007 08:55 GMT
Matt,
Actually there is simple way get the WB downloaded:
Dim URL As String
Dim WB As Workbook
'Build your URL as required from symbol, start/end date etc from cell value
or where ever to give something like:
URL =
"http://ichart.finance.yahoo.com/table.csv?s=LLTC&d=0&e=26&f=2007&g=d&a=2&b=
26&c=1990&ignore=.csv"
Set WB = Workbooks.Open(URL)
WB.SaveAs "C:\Some name.xls"
NickHK
> Nick,
>
[quoted text clipped - 174 lines]
> >
> > Looking at the URL that actual sent for a particular date range:http://finance.yahoo.com/q/hp?s=LLTC&a=02&b=26&c=1990&d=00&e=24&f=200.
..
> > and allowing for the extra pages with the z and y parametershttp://finance.yahoo.com/q/hp?s=LLTC&a=02&b=26&c=1990&d=00&e=24&f=
200...
> > So you can:
> > - Amend the web query's Connection string
[quoted text clipped - 131 lines]
> >
> > > End Function- Hide quoted text -- Show quoted text -