I am trying to automatically grab various stock data from Yahoo finance. I wrote a macro for each page I want to grab data from, for example:
http://finance.yahoo.com/q/ks?s=IT or
http://finance.yahoo.com/q/is?s=IT&annualThe macros first clear out the target worksheet, including the query from a previous run of the macro and then designates that the data is to be returned to cell A1, and in fact it selects A1 before going onto creating the web query for the current iteration. At the end of this message is an example of the macro for the first sample page listed above
There are about 10 web pages I am trying to get data from, and their is a macro for each page, and I wrote one macro to run each of these 10 individual macros. (I am concerned that the ubber macro is going onto a new page before the previous query is completed and this may add some headaches and would like your imput on this)
Anyways, some tables are grabed and placed in their worksheets as designed every time. But in other worksheets, such as the pages listed above and in the macro example included, the performance is much more mixed. In some cases they work fine, but other times some combination of 1) the data is returned to the wrong location within the worksheet 2) the wrong table or Item from the page is returned 3) no data is returned 4) only part of the desired table is returned.
When I went into the edit query function, sometimes the correct tables are marked for importation, some times they are not. There does not seem a pattern. I dont understand why this is happening, I recored each of these macros and they worked fine before I save the macro, so it should be returning the same data, the same way each time, but this is not happening.
I searched the MS KB and they mentioned that Dynamically Scripted Tables may be interfering with the query, if this is the case is there a solution to this problem?
Would it be better to right a macro/query for each individual table I want to grab? Though there are problems with macros/queries associated with pages where I am only going after a single table on that particular page.
The key is to retrieve the right data reliably to the correct location, any help would be greatly apprecciated.
Example Macro-
Sub keystats()
'
' keystats Macro
' Macro recorded 10/18/2004 by Michael J. Palma
'
'
'Setup worksheet
Sheets("keystats").Select
Cells.Select
Selection.clear
Selection.QueryTable.Delet
e
' Range("A:A,B:B").Select
' Range("B1").Activate
' Selection.Delete Shift:=xlToLeft
' Range("A1").Select
' Selection.EntireColumn.Ins
ert
' Selection.EntireColumn.Ins
ert
Range("A1").Select
'
With ActiveSheet.QueryTables.Ad
d(Connecti
on:= _
"URL;
http://finance.yahoo.com/q/ks?s=IT", Destination:=Range("A1"))
.Name = "ks?s=IT_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "26,31,34,37,40,43,46,53,5
6,59"
.WebPreFormattedTextToColu
mns = True
.WebConsecutiveDelimitersA
sOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition
= False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub