'Declare variablesDim symbolrange As Range
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim url1 As String
Dim url2 As String
Dim ie As InternetExplorer
Sub main1()
'create an inputbox for user to specify range
On Error GoTo Handler 'if you click cancel on the pop up, exit macro
Set symbolrange = Application.InputBox _
("Select Range Containing Stock Symbols", _
"Select Range", Selection.Address(0, 0), Type:=8)
On Error GoTo 0 'Disable the error handler that we turned on above
Call setdate 'A macro to create part of the URL
Call setupconnection 'A macro to set up connection
Call grabdata 'A macro to loop data connection
Handler:
End Sub
Sub setdate()
'This macro converts date into partial URL
startdate = Range("Start_Date").Value
enddate = Date - 1
'Check if Startdate is correct
If startdate >= enddate Then
MsgBox "Your start date is later than your end date"
End
End If
'Check if startdate is too early
If enddate - startdate > 5000 Then
yesno = MsgBox _
("Confirm that your source supports this date range", vbYesNo)
If yes = vbNo Then End
End If
'If above checks are ok continue below
startmonth = WorksheetFunction.Text(Month(startdate) - 1, "00")
startday = Day(startdate)
startyear = Year(startdate)
startdate1 = "&a=" & startmonth & "&b=" & startday & "&c=" _
& startyear
startmonth = WorksheetFunction.Text(Month(enddate) - 1, "00")
startday = Day(enddate)
startyear = Year(enddate)
enddate1 = "&d=" & startmonth & "&e=" & startday & "&f=" _
& startyear
'url1 forms the partial url that dictates the date range
url1 = startdate1 & enddate1
End Sub
Sub setupconnection()
'On error go to line with the words error1
On Error GoTo error1
Set ws = ActiveSheet
'Create a new spreadsheet to store connection
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = _
"Data Connection" & Worksheets.Count
Set ws1 = ActiveSheet
'Set url2 to the url you are surfing to
'REMEMBER to change goohoo.com to the real
'website
url2 = "http://finance.goohoo.com/q/hp?s=" & _
symbolrange(1, 1) & url1 & "&g=d&z=66&y=" & 0
'Add a connection
'REMEMBER to change goohoo.com to the real
'website
With ws1.QueryTables.Add(Connection:= _
"URL;http://finance.goohoo.com/q/hp?s=" & _
symbolrange(1, 1) & url1 & "&g=d&z=66&y=" & _
0, Destination:=ws1.Range("$A$1"))
.Name = "Hist_Data"
.FillAdjacentFormulas = False
.WebSelectionType = xlSpecifiedTables
.WebTables = "20"
.BackgroundQuery = False
.Refresh BackgroundQuery:=False
'You can remove the line for excel to wait if you
'successfully disable background query.
'Making Excel wait 3 seconds is very long
Application.Wait (Now() + TimeValue("00:00:03"))
End With
On Error GoTo 0
'Exit sub to prevent the macro from
'running the lines after error1 when
'there is no error.
Exit Sub
error1:
'Clear temp files
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 "
Resume
End Sub
Sub grabdata()
On Error GoTo error1
For Each rcell In symbolrange
'Create a new spreadsheet to store each stock's data
Sheets.Add(After:=Worksheets(Worksheets.Count)).Name = _
rcell.Value & Worksheets.Count
Set ws2 = ActiveSheet
'Create a loop to keep grabbing data until complete
stock1 = rcell.Value
a = -1
Do
a = a + 1
'Remember to change goohoo.com
url2 = "http://finance.goohoo.com/q/hp?s=" & _
rcell.Value & url1 & "&g=d&z=66&y=" & a * 66
With ws1.QueryTables("Hist_Data")
.Connection = "URL;" & url2
.Refresh BackgroundQuery:=False
'You can remove the line for excel to wait if you
'successfully disable background query.
'Making Excel wait 3 seconds is very long
Application.Wait (Now() + TimeValue("00:00:03"))
End With
'Copy data onto correct spreadsheet
lastrow = ws2.Cells(65000, 1).End(xlUp).Row
If a = 0 Then
ws1.Range("Hist_Data").Copy
ws2.Cells(lastrow, 1).PasteSpecial xlPasteValuesAndNumberFormats
Else
ws1.Range("Hist_Data").Offset(1, 0).Copy
ws2.Cells(lastrow, 1).PasteSpecial xlPasteValuesAndNumberFormats
End If
Application.CutCopyMode = False
lastrow = ws2.Cells(65000, 1).End(xlUp).Row
Loop Until ws1.Cells(1, 1).Value = ws2.Cells(lastrow, 1).Value
Next rcell
On Error GoTo 0
'Exit sub to prevent the macro from
'running the lines after error1 when
'there is no error.
Exit Sub
error1:
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 "
Resume
End Sub