Excel- How to Pull Beta for Stock from Yahoo Finance

cwang

New Member
Joined
May 28, 2014
Messages
12
Hi all,

I'm completely new to macros, VBA. I'm wondering if anyone knows how to pull the beta for a specific stock symbol from yahoo finance onto the excel page.

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
    Dim xHttp As Object
    Dim t, ticker  As String
    Dim beta As Single
    
    Set xHttp = CreateObject("Microsoft.XMLHTTP")
    
    ticker = "intc"
    
    xHttp.Open "GET", "http://finance.yahoo.com/q?s=" & ticker & "&ql=1", False
    xHttp.Send

    t = xHttp.responseText
    
    t = Mid(t, InStr(t, ">Beta:<") + 1)
    t = Mid(t, InStr(t, ">") + 1)
    t = Mid(t, InStr(t, ">") + 1)
    
    beta = Val(Left(t, InStr(t, "<") - 1))
 
Upvote 0
Or you could create a user defined function. To use it, put a stock symbol in a cell. For example, put INTC in cell A1. Then add the function to a different cell. For example, put this in cell B1:

=GetBeta(A1)


Code:
Function GetBeta(ByVal tRange As Range) As Single

    Dim xHttp As Object
    Dim t, ticker  As String
    
    Set xHttp = CreateObject("Microsoft.XMLHTTP")
    
    ticker = tRange.Text
    
    xHttp.Open "GET", "http://finance.yahoo.com/q?s=" & ticker & "&ql=1", False
    xHttp.Send

    t = xHttp.responseText
    
    t = Mid(t, InStr(t, ">Beta:<") + 1)
    t = Mid(t, InStr(t, ">") + 1)
    t = Mid(t, InStr(t, ">") + 1)
    
    GetBeta = Val(Left(t, InStr(t, "<") - 1))

End Function
 
Upvote 0
Thanks so much guys... tbh I have no idea what any of that means hah... this is kind of asking a lot but could someone walk me through- not the code but the applicability)

Do I go to Developer-> Visual Basic-> and then what?

Say I want the beta to appear in a certain cell too
 
Upvote 0
Open the Visual Basic editor. You can do that through the menus or ribbon bar, or you can do it by pressing ALT-F11.

On the upper-left side of the editor you should see something that says Project - VBAProject. Below that in a window you should see something that says ThisWorkbook. Right click on that and insert a module. There should now be something below ThisWorkbook names Module1. Double click on that. In a large window to the right of the Project - VBAProject window is where you will be pasting the code provided above (the second post by me, that has the GetBeta function). The large window where you will paste the data should have 2 dropdowns above it. One says (General) and the other says (Declarations).

Once you have pasted the code in that window, go back to the Excel workbook and put a stock symbol in cell A1. In cell B1 enter the formula:

=GetBeta(A1)

Cell B1 should now contain the beta value for the stock.
 
Upvote 0
Thanks so much mjbeam. I'm trying to learn VBasic but I need these codes ASAP.

Two more questions.

1) What if I want to get the Total Debt/Equity (https://finance.yahoo.com/q/ks?s=X+Key+Statistics)
2) Whats a quick and dirty way to learn how to program Vbasic ? Or rather read the language. I had experience in java 4 years ago. Forgot a good amount
 
Upvote 0
In the code provided above, where it says t = xHttp.responseText, once this line is executed, the variable t will hold the HTML of the Yahoo webpage with all of the stock information. You will need to extract the information you need from t. You can get an idea of what the contents of t are going to look like by going to the website in your browser and right clicking on it somewhere and selecting "View Source" from the context menu that pops up. Then search for what you want to extract, in this case if I search for "Total Debt/Equity (mrq):" this is the data that will be around it in the variable t:


Code:
class="yfnc_tablehead1" width="74%">Total Debt/Equity (mrq):< /td>< td class="yfnc_tabledata1">114.11< /td>< /tr>< tr>< td

So, I can trim off the stuff I don't need before the data I'm looking for like this:

Code:
t = mid(t, instr(t, "Total Debt/Equity (mrq):"))

Then t will look like this:

Code:
Total Debt/Equity (mrq):< /td>< td class="yfnc_tabledata1">114.11< /td>< /tr>< tr>< td

Now to get to the 114.11I can strip off everything in t that comes before a ">" character 2 times like this:

Code:
t = mid(t, instr(t, ">")+1)
t = mid(t, instr(t, ">")+1)

...and t will look like this:

Code:
114.11< /td>< /tr>< tr>< td

Now I'll strip off everything after and including the first "<" in t:

Code:
t = left)t, instr(t, "<")-1)

Now t holds the string, 114.11. We need to convert it into a number before we return it to the spreadsheet:

Code:
t = val(t)


To extract the rest of the stuff you want to extract you just need to do some string manipulation on the variable t in the code I provided earlier. Also, you will need to modify thhe xHTTP.Open line to use the new URL you provided:

Code:
xHttp.Open "GET", "https://finance.yahoo.com/q/ks?s=" & ticker & "+Key+Statistics", False

One other thing, each cell that has this formula is doing a unique query from a Yahoo server so don't over do it. This is not the best way to go if you are needing to pull thousands of stock quotes. And, if you need to pull multiple pieces of information for each stock, this is also not the best way to achieve that. It would be better to pull all the pieces of information in one function and return the results to multiple cells. The reason is because you don't want to do 10 queries to Yahoo to get the same web page but extract a different piece of information from each query. You want to do one query and extract all the info at once, then return those results to multiple cells on the worksheet.


Hope this helps.


To extract Total Dept/Equity (new function name is GetTDE):

Code:
 Function GetTDE(ByVal tRange As Range) As Single

    Dim xHttp As Object
    Dim t, ticker  As String
    
    Set xHttp = CreateObject("Microsoft.XMLHTTP")

    ticker = tRange.Text
    
    xHttp.Open "GET", "https://finance.yahoo.com/q/ks?s=" & ticker & "+Key+Statistics", False
    xHttp.Send

    t = xHttp.responseText
    
    t = Mid(t, InStr(t, "Total Debt/Equity (mrq):"))
    t = Mid(t, InStr(t, ">") + 1)
    t = Mid(t, InStr(t, ">") + 1)
    
    GetTDE = Val(Left(t, InStr(t, "<") - 1))

End Function
 
Last edited:
Upvote 0
Thanks mjbeam.

Had I not had your help I would've been screwed haha. How long would it have taken me to learn/develop this macro myself if I have 0 knowledge of VBasic? Is this more complicated VBasic?

And is it complicated to do this:

"One other thing, each cell that has this formula is doing a unique query from a Yahoo server so don't over do it. This is not the best way to go if you are needing to pull thousands of stock quotes. And, if you need to pull multiple pieces of information for each stock, this is also not the best way to achieve that. It would be better to pull all the pieces of information in one function and return the results to multiple cells. The reason is because you don't want to do 10 queries to Yahoo to get the same web page but extract a different piece of information from each query. You want to do one query and extract all the info at once, then return those results to multiple cells on the worksheet."
 
Upvote 0
It is not complicated to pull multiple values for a particular stock, it's just tedious. If you know that you want to pull more than one item from the page per stock I could convert this into an array function.

If you are wanting to pull a dozen or so stocks, this will work fine. If you need to pull hundreds or thousands of stocks, you should find a website that lists this information for multiple stocks on a single page and extract it that way.
 
Upvote 0
Do you know whats wrong with my code if I want to pull market cap? I tried modifying your code:

Function GetMC(ByVal tRange As Range) As Single


Dim xHttp As Object
Dim t, ticker As String

Set xHttp = CreateObject("Microsoft.XMLHTTP")

ticker = tRange.Text

xHttp.Open "GET", "http://finance.yahoo.com/q?s=" & ticker & "&ql=1", False
xHttp.Send


t = xHttp.responseText

t = Mid(t, InStr(t, ">Market Cap:<"))
t = Mid(t, InStr(t, ">") + 1)
t = Mid(t, InStr(t, ">") + 1)

GetMC = Val(Left(t, InStr(t, "<") - 1))


End Function
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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