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:
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