A quick question about the amending of a piece of code to pick up other web page table contents. I had some great guidance on here about how to pick up certain table contents based on the title and it works great - thanks again to 'QHarr' who was so helpful.
The URL I was wanting the table details from was:
https://finance.yahoo.com/quote/AAPL/?p=AAPL
This is the piece of code which took the data I wanted:
Sub GetYahooInfo100()
Dim tickers(), ticker As Long, lastRow As Long, headers()
Dim wsSource As Worksheet, http As clsHTTP, html As HTMLDocument
Application.ScreenUpdating = False
Set wsSource = ThisWorkbook.Worksheets("100")
Set http = New clsHTTP
headers = Array("Ticker", "Previous Close", "Open", "Bid", "Ask", "Day's Range", "52 Week Range", "Volume", "Avg. Volume", "Market Cap", "Beta", "PE Ratio (TTM)", "EPS (TTM)", _
"Earnings Date", "Forward Dividend & Yield", "Ex-Dividend Date", "1y Target Est")
With wsSource
lastRow = GetLastRow(wsSource, 1)
Select Case lastRow
Case Is < 3
Exit Sub
Case 3
ReDim tickers(1, 1): tickers(1, 1) = .Range("A3").Value
Case Is > 3
tickers = .Range("A3:A" & lastRow).Value
End Select
ReDim Results(0 To UBound(tickers, 1) - 1)
Dim i As Long, endPoint As Long
endPoint = UBound(headers)
For ticker = LBound(tickers, 1) To UBound(tickers, 1)
If Not IsEmpty(tickers(ticker, 1)) Then
Set html = http.GetHTMLDoc("https://finance.yahoo.com/quote/" & tickers(ticker, 1) & "/?p=" & tickers(ticker, 1))
Results(ticker - 1) = http.GetInfo(html, endPoint)
On Error Resume Next
Set html = Nothing
Else
Results(ticker) = vbNullString
End If
Next
.Cells(2, 1).Resize(1, UBound(headers) + 1) = headers
For i = LBound(Results) To UBound(Results)
.Cells(3 + i, 2).Resize(1, endPoint - 1) = Results(i)
Next
End With
Application.ScreenUpdating = True
End Sub
With a further piece below:
Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row
End With
End Function
Like I say one of the members on here was very helpful in identifying how to do that. I have tried in vein to amend the piece of code to pick up another set of data from the statistics page, as follows:
https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL
but I must be missing something. Unless my references are incorrect for the tables then I'm at a loss. I am looking to see if I can capture all the data fields in each of the tables, not just one which is where I may be going wrong.
Was hoping someone may be able to assist.
With forward thanks.
