Looking to copy Stock options data from Barcharts.com and paste into Excel sheet.
Sub CopyTables()
Dim ie As Object
Dim I As Long
I = 0
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "https://www.barchart.com/stocks/quotes/GOOG/options?moneyness=allRows&view=sbs&expiration=2018-02-23"
ie.Visible = True
Do While ie.Busy And Not ie.readyState = 4
DoEvents
Loop
DoEvents
Set tables = ie.document.getElementsByTagName("table")
SetDataFromWebTable tables, Range("B5")
ie.Quit
End Sub
Also how would I extract the dates from the webpage dropdown "Expiration" and paste them all into Excel as well?
I have searched for something that will work for me, no luck!
The webpage source HTML by the link provided
https://www.barchart.com/stocks/quotes/GOOG/options?moneyness=allRows&view=sbs&expiration=2018-02-23
doesn't contain the necessary data, it uses AJAX. The website https://www.barchart.com has an API available. Response is returned in JSON format. Navigate the page e. g. in Chrome, then open Developer Tools window (F12), Network tab, reload (F5) the page and examine logged XHRs. Most relevant data is JSON string returned by the URL:
https://core-api.barchart.com/v1/options/chain?symbol=GOOG&fields=optionType%2CstrikePrice%2ClastPrice%2CpercentChange%2CbidPrice%2CaskPrice%2Cvolume%2CopenInterest&groupBy=strikePrice&meta=field.shortName%2Cfield.description%2Cfield.type&raw=1&expirationDate=2018-02-23
You may use the below VBA code to retrieve info as described above. Import JSON.bas module into the VBA project for JSON processing.
The output for me is as follows:
To make output closer to Side-by-Side view on the webpage, you may slightly play with query parameters:
And also change the line
In that case the output is as follows:
BTW, the similar approach applied in other answers.