Re: HELP! VBA Rookie, average excel user Help me save some
Hello Marc, I had a list of valid tickers in Column A and ran the following with some success:
<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> YippieYahoo()
<SPAN style="color:darkblue">Dim</SPAN> ie <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Object</SPAN>, cTables <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Object</SPAN>, Table <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Object</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> MyArr <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Variant</SPAN>, cl <SPAN style="color:darkblue">As</SPAN> Range, y <SPAN style="color:darkblue">As</SPAN> Worksheet, Tick <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>
<SPAN style="color:darkblue">Set</SPAN> ie = CreateObject("InternetExplorer.Application")
<SPAN style="color:darkblue">With</SPAN> Application
.ScreenUpdating = <SPAN style="color:darkblue">False</SPAN>
.DisplayAlerts = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">With</SPAN> ie
<SPAN style="color:darkblue">For</SPAN> <SPAN style="color:darkblue">Each</SPAN> cl <SPAN style="color:darkblue">In</SPAN> Range([a1], [a65536].End(3))
Tick = cl.Value
.Navigate "http://finance.yahoo.com/q?s=" & Tick
<SPAN style="color:darkblue">Do</SPAN> <SPAN style="color:darkblue">While</SPAN> .Busy And .ReadyState <> 4
DoEvents
<SPAN style="color:darkblue">Loop</SPAN>
<SPAN style="color:darkblue">With</SPAN> .document
<SPAN style="color:darkblue">Set</SPAN> cTables = .all.tags("table")
<SPAN style="color:darkblue">Set</SPAN> y = ThisWorkbook.Worksheets.Add(after:=Sheets(ThisWorkbook.Sheets.Count))
y.Name = Tick: y.[a1] = Tick
<SPAN style="color:darkblue">For</SPAN> <SPAN style="color:darkblue">Each</SPAN> Table <SPAN style="color:darkblue">In</SPAN> cTables
<SPAN style="color:darkblue">If</SPAN> Left$(Table.Rows(0).Cells(0).innerText, 10) = "Last Trade" And _
Len(Table.Rows(0).Cells(0).innerText) > 11 <SPAN style="color:darkblue">Then</SPAN>
MyArr = Evaluate("{""" & _
Application.Substitute(Table.Rows(0).Cells(0).innerText, _
vbNewLine, """;""") & """}")
y.[a2].Resize(UBound(MyArr)) = MyArr
y.[a2:a65536].TextToColumns Destination:=y.[a2], DataType:=xlDelimited, _
Other:=True, OtherChar:=":", _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1))
y.[b3] = y.[b3] & y.[c3]
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">If</SPAN> Left$(Table.Rows(0).Cells(0).innerText, 11) = "Day<SPAN style="color:green">'s Range" Then</SPAN>
MyArr = Evaluate("{""" & _
Application.Substitute(Table.Rows(0).Cells(0).innerText, _
vbNewLine, """;""") & """}")
y.[c2].Resize(UBound(MyArr)) = MyArr
y.[c2:c65536].TextToColumns Destination:=y.[c2], DataType:=xlDelimited, _
Other:=True, OtherChar:=":", FieldInfo:=Array(1, 1)
<SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">For</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">Next</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">Set</SPAN> y = Nothing: <SPAN style="color:darkblue">Set</SPAN> cTables = <SPAN style="color:darkblue">Nothing</SPAN>
<SPAN style="color:darkblue">Next</SPAN>
.Quit
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">Set</SPAN> ie = <SPAN style="color:darkblue">Nothing</SPAN>
<SPAN style="color:darkblue">With</SPAN> Application
.ScreenUpdating = <SPAN style="color:darkblue">True</SPAN>
.DisplayAlerts = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
At first I was simply thinking of grabbing CTables(17), but with some limited testing, it seemed that the number of tables early in the document was changing with the ticker, so it would work for some issues and not for others, in any case, hence the table loop.
Again, not very much testing, and virtually no error handling, use at your own risk. And I wouldn't run it for 100+ at one time, it will take a very long time...
Hope this helps.