I need to read the closing price of a stock from the Yahoo Finance page. I had this answered before using Google Finance page, but the page is no longer available and I believe
Maybe you have an aversion to google, but I think you should consider using this small utility.
http://investexcel.net/multiple-stock-quote-downloader-for-excel/
That should do everything you want and a while lot more!!
Otherwise, in Excel, under the Data tab, click Existing Connections and then click MSN Money Central Investor Stock Quotes. See the image below. Enter your tickers/symbols, and click Open.
If the app is not installed already, click the link below, and follow the steps to get everything setup and working on your machine.
https://appsource.microsoft.com/en-us/product/office/WA104379220?tab=Overview
Try the below way. It should fetch you the value of AAPL
from https://finance.yahoo.com/quote/
. To reach the value using class
name or tag
name is in reality troublesome. However, I used static id
here.
Sub Fetch_Quote()
Dim HTML As HTMLDocument, elem As Object, URL$
URL = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
Set elem = HTML.getElementById("quote-market-notice").PreviousSibling.PreviousSibling
MsgBox elem.innerText
End With
End Sub
Then try this. Now, you should get the result with the blink of an eye:
Sub Fetch_Quote()
Dim HTML As New HTMLDocument, elem As Object, URL$
URL = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", URL, False
.send
HTML.body.innerHTML = .responseText
Set elem = HTML.getElementById("quote-market-notice").PreviousSibling.PreviousSibling
MsgBox elem.innerText
End With
End Sub
Reference to add to the library:
Microsoft XML, V6.0