Context: This code fetches share prices on Yahoo Finance for a list of stocks. It used to work fine, but now it indicates this Error 91, but only for a few of them. I checked, and the code was not mistakenly changed.
Error is indicated at the line: "price = html.getElementsByClassName ("Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)") .Item(0) .innerText
The full code is: (example: LVMH)
Sub LVMH
Dim request As Object
Dim response as String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
website = "https://finance.yahoo.com/quote/MC.PA"
Set request = CreateObject ("MSXML2.XMLHTTP")
request .Open "GET", website, False
request .setRequestHeader "if-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request .send
response = StrConv(request.responseBody, vbUnicode)
html.body.innerHTML = response
price = html.getElementsByClassName ("Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)") .Item(0) .innerText
Sheets("Sheet1") .Range("H30") .Value = price
End Sub
If it helps, the creator of the video, and this website (I believe) replied it might be because the Yahoo finance website changed a bit.
I attach a sample file.
Thank you very much!
posted by:
PPal on
Tue Aug 11, 2020 at 10:38 am