Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. 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)....


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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Because this line doesn't change and therefore executes all the time:
If HTMLtd.getAttribute("data-test") = "BID-value"
 
Upvote 0
How does this affect the results. It seems after the market open and the prices are updated, the prices results in 0. However, now that market is closed, the prices are update correctly. Any correlation? Thanks. How can this be corrected?
 
Upvote 0
How does this affect the results. It seems after the market open and the prices are updated, the prices results in 0. However, now that market is closed, the prices are update correctly. Any correlation? Thanks. How can this be corrected?

Like I pointed out in previous posts #8 and #10 in another thread you should give us more information. The change that you get help is bigger. And perhaps more people from this forum will participate. Also, try to stay in the same topic because the nature of your question is the same.

So, please look at the questions asked in above mentioned posts.
- Can you provide us the desired results?
- What values are you after?
- What are the values in columns A, G, H, E because you use these values to create the url. Now we have to guess.

When I go to this url:
https://finance.yahoo.com/quote/AAP181221C00170000?p=AAP181221C00170000

I get these values.
- Are these the values you are after?
 
Upvote 0
- Can you provide us the desired results?

The desired result is to get the Bid Price, which the code accomplishes this task. However, I notice when I execute the code when the market is open and the prices are updated, the code returns 0. Now that the market is closed the code is returning the correct Bid Price. Not sure why? Is it possible an explanation can be provided?

What are the values in columns A, G, H, E because you use these values to create the url. Now we have to guess.

A = AAPL
G = $175.00
H = 01/18/2018
E = PUT

AAPLApple Inc.11/23/20182 Put5 $ 175.0001/18/2019

<tbody>
</tbody>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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