Av8tordude
Well-known Member
- Joined
- Oct 13, 2007
- Messages
- 1,074
- Office Version
- 2019
- Platform
- Windows
The vba codes below are similar in scope, except the first code only looks up one price, however the second code is suppose to loop through each row to get each price in the range. However, it does not accomplish the task. I'm keeping getting the same numbers each time I run the second code. Can someone explain... Thank you kindly.
Getting price (Without Loop)....
Getting price (With Loop)....
Getting price (Without Loop)....
Code:
URL = "https://finance.yahoo.com/quote/" & Range("A6") & Format(Range("H6"), "yymmdd") & Left(Range("E6"), 1) & Format(Range("G6") * 1000, "00000000") & "?p=" & Range("A6") & Format(Range("H6"), "yymmdd") & Left(Range("E6"), 1) & Format(Range("G6") * 1000, "00000000")
XMLPage.Open "GET", URL, False XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText 'Get the source (code) of the webpage . . .
Set HTMLtables = HTMLDoc.getElementsByTagName("table") 'Set reference to all tables in htmlDoc.body.innerHTML . . .
For Each HTMLtable In HTMLtables 'Loop through all table-tags . . .
If HTMLtable.className = "W(100%)" Then '.and search for classname "W(100%) . . ."
Set HTMLtds = HTMLDoc.getElementsByTagName("td") 'When found, set reference to all td elements in table . . .
For Each HTMLtd In HTMLtds 'Loop through all td elements . . .
If HTMLtd.getAttribute("data-test") = "BID-value" Then 'and search for the attribute "data-test" with value "BID-value" . . .
Range("M" & i) = HTMLtd.innerText 'When found, print value to immediate window . . .
End If
Next HTMLtd
End If
Next HTMLtable
Getting price (With Loop)....
Code:
For i = 3 To 12
If Range("A" & i).Value <> "" Then
URL = "https://finance.yahoo.com/quote/" & Range("A" & i) & Format(Range("H" & i), "yymmdd") & Left(Range("E" & i), 1) & Format(Range("G" & i) * 1000, "00000000") & "?p=" & Range("A" & i) & Format(Range("H" & i), "yymmdd") & Left(Range("E" & i), 1) & Format(Range("G" & i) * 1000, "00000000")
XMLPage.Open "GET", URL, False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText 'Get the source (code) of the webpage . . .
Set HTMLtables = HTMLDoc.getElementsByTagName("table") 'Set reference to all tables in htmlDoc.body.innerHTML . . .
For Each HTMLtable In HTMLtables 'Loop through all table-tags . . .
If HTMLtable.className = "W(100%)" Then '.and search for classname "W(100%) . . ."
Set HTMLtds = HTMLDoc.getElementsByTagName("td") 'When found, set reference to all td elements in table . . .
For Each HTMLtd In HTMLtds 'Loop through all td elements . . .
If HTMLtd.getAttribute("data-test") = "BID-value" Then 'and search for the attribute "data-test" with value "BID-value" . . .
Range("M" & i) = HTMLtd.innerText 'When found, print value to immediate window . . .
End If
Next HTMLtd
End If
Next HTMLtable
End if
Next i