Transferring Data from the Web Page to an Excel Worksheet Using VBA
source link: https://www.codesd.com/item/transferring-data-from-the-web-page-to-an-excel-worksheet-using-vba.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Transferring Data from the Web Page to an Excel Worksheet Using VBA
This is my first post. I am new at VBA but I'm pretty familiar with VB6. I wrote some code that takes the text from nasdaq and pastes it into a worksheet. It finally works. There is a lot of extraneous data scattered around above and below The Annual Income Statement. I would like to parse out and place the important data in a place where I can automate analysis. I'm thinking that I could search the cells until I find: Annual Income Statement and extract to a different sheet. Any suggestions would be very appreciated. Here's what I've got:
Sub TransferWebData()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "http://www.nasdaq.com/symbol/gd/financials"
Do Until .ReadyState = 4: DoEvents: Loop
IE.ExecWB 17, 0 'SelectAll
IE.ExecWB 12, 2 'Copy selection
Sheets("GD").Range("A1").Select
Sheets("GD").PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
IE.Quit
End With
End Sub
THis should keep you busy.
Set references to Microsoft HTML Object Library and Microsoft Internet Controls.
In Google Chrome I navigated to the webpage and use inspect element to open WebKit and copy the xpath to the element. This gave me and outline to compose a rough draft of my function. After an hour and a half of tedious debugging I was able to extract the data into an array.
Sub TransferWebData()
Dim Data
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "http://www.nasdaq.com/symbol/gd/financials"
Do Until .ReadyState = 4: DoEvents: Loop
Data = getFinancialsArray(IE.document)
With Worksheets("GD")
.Cells.ClearContents
.Range("A1").Resize(UBound(Data, 1) + 1, UBound(Data, 2)).Value = Data
.Columns.AutoFit
End With
IE.Quit
End With
End Sub
' //*[@id="financials-iframe-wrap"]/div[1]/table/tbody/tr[1]/td[2]
Function getFinancialsArray(doc As HTMLDocument)
Dim Data
Dim x As Long, y As Long, y1 As Long
Dim divfinancials As HTMLDivElement, div1 As HTMLDivElement
Dim tbl As HTMLTable, allRows
Set divfinancials = doc.getElementById("financials-iframe-wrap")
Set div1 = divfinancials.getElementsByTagName("div").Item(0)
Set tbl = div1.getElementsByTagName("table").Item(0)
Set allRows = tbl.getElementsByTagName("tr")
Dim s As String
ReDim Data(allRows.Length, 10)
For y = 0 To allRows.Length - 1
If Len(Trim(allRows.Item(y).innerText)) Then 'If the row has data
For x = 0 To allRows.Item(y).Cells.Length - 1
Data(y1, x) = allRows.Item(y).Cells(x).innerText
Next
y1 = y1 + 1
End If
Next
getFinancialsArray = Data
End Function
Output
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK