HELP! VBA Rookie, average excel user Help me save some time

mjansen

New Member
Joined
Jan 23, 2004
Messages
20
Maybe someone can help me. I ahve a list of 156 companies and their stock tickers, and what I would like to do is have a script that will take the stickticker symbols and use those to query yahoo finance for it's data tables, specifically those found on the key statics page, create a new sheet labeled with the ticker (so a sheet for every ticker) and then paste the table data into the newly created sheet.

Any suggestions? WHere should I start.... or is this impossible?

Thanks for all your hel pso far,

Marc

:help:
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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. :)
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

Thanks for the help Nate,

This gives me something to start with. I'm also looking to grab information from the Key Statistics page so for instance for Microsoft the url is http://finance.yahoo.com/q/ks?s=msft . I'll play with what you have so far, although I really don't know what I'm doing, but's there nothing betetr to learn through doing.

Thanks again, and I'm up for any other help out there,

Marc
:pray:
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

NateO,

The code you posted works an absolute treat!

I am hoping I can borrow your expertise on a variation to the script.

I have an Excel file with the following sheets:

Sheet 1 - Codes
Column A contains a list of stock codes
Column B contains prices accessed from Sheet 2

Sheet 2 - Data
Contains table manually pasted from: http://au.finance.yahoo.com/q?m=a&s=aln+amp+asx&d=v1

I would love to automate the data importing into Excel (sheet 2).

Any assistance in getting this up and running will be greatly appreciated. I have had a go at modifying your code but am a little stuck on what it all means.

Thanks again!

:beerchug:
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

Hello,

Which table? Are you looping like the original request? This page looks like it concatenates tickers.

This may not be the best block of code to get your feet wet with... Have you tried a web query via MS Query?

Also, duplicate threads convolute the discussion, I will delete the 2nd thread.
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

I was thinking it could be done like follows:

1) Sheet 1, Column A contains list of codes

2) Macro runs

Macro puts Column A into the URL

http://au.finance.yahoo.com/q?m=a&s=A1:A1000&d=v1

3) URL runs and spits out the below table

http://au.finance.yahoo.com/q?m=a&s=amp+aln+tls&d=v1

Symbol Name Last Trade Change Volume More Info
AMP.AX AMP 11:29am 5.840 +0.010 +0.17% 1,880,132 Chart, News, Profile, Research
ALN.AX ALINTA 11:29am 6.170 +0.040 +0.65% 488,227 Chart, News, Profile, Research
TLS.AX TELSTRA CORP FPO 11:30am 4.600 -0.040 -0.86% 7,791,060 Chart, News, Profile, Research

4) Macro then copies table into Sheet 2

Some how copy from "Symbol" to "Add to my Portfolio". Column D will always hold the last price data.

5) Then Sheet 1 Column B

Column B uses a Vlookup to match Column A with the price from sheet 2


I guess my whole objective if to try and import the table of data neatly into Excel. I have tried a web query, but the cells tend to move around a lot and pull in a lot of garbage as well.

Hoping you can help.
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

I have made some developments in my project but have run into some problems.

I have created a web query called "Yahoo Finance.iqy":

Rich (BB code):
WEB
1
http://au.finance.yahoo.com/q?m=a&d=v1&s=["QUOTE","Enter Codes"]

Selection=10
Formatting=False
PreFormattedTextToColumns=False
ConsecutiveDelimitersAsOne=True
SingleBlockTextImport=False

I run the query and the stock price table is imported into a new sheet. I just have some questions in getting everything to run smoothly:

1)

How can ensure that the query always pulls in the right table. "Selection=10" assumes that the 10th table is the table that contains quotes. Is there a way to search for the right table (ie the table with the prices)?

2)

How can I remove all fortmatting from the imported data?

3)

Is there a website that lists the variables available for web queries.

Selection=
Formatting=
PreFormattedTextToColumns=
ConsecutiveDelimitersAsOne=
SingleBlockTextImport=

4)

I am using a VLOOKUP function to pull the price from the imported data into a new sheet but Excel spits out the data in the cell above the 'matched cell':

Cell A2 contains ALN
Cell B2 contains =VLOOKUP(A2,IMPORTED_DATA!A1:K14,4,1)

Returns the price from the cell above?


If any of the above needs clarification feel free to let me know. Not to sure what I am doing :-(
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

How can ensure that the query always pulls in the right table. "Selection=10" assumes that the 10th table is the table that contains quotes. Is there a way to search for the right table (ie the table with the prices)?
Yahoo seems to be a moving target. If this is the case, I think a loop with OLE automation is requried.

I don't see a table name in the source, does 10 not always work?

Also, when you say a list of tickers do you mean like:
Book3
ABCD
1ALN.AX
2AMP.AX
3ASX.AX
Sheet2


Where the tickers are concatenated for the resulting table?

Also, have you seen these:

http://www.mrexcel.com/board2/viewtopic.php?t=56809&start=2
 
Upvote 0
Re: HELP! VBA Rookie, average excel user Help me save some

>>> Yahoo seems to be a moving target. If this is the case, I think a loop
>>> with OLE automation is requried.
>>>
>>> I don't see a table name in the source, does 10 not always work?

Sometimes 10 doesn't work. How can I name the table? How do I create this loop?

>>> Also, when you say a list of tickers do you mean like....

Yep, exactly.

>>> Where the tickers are concatenated for the resulting table?

Each value was in a separate cell but same values fell into the same columns.

http://au.finance.yahoo.com/q?m=a&s=aln+amp&d=v1

>>> Also, have you seen these:
>>> http://www.mrexcel.com/board2/viewtopic.php?t=56809&start=2

I had a look, but the codes seem to pull the whole page into Excel. I am trying to grab the table only.

:oops:
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top