Hi
I've done a lot with VBA over the years but never used VBA to scrape data using IE or HTML.
I tried using a number of methods posted within the forum but every time I run into a problem I cannot solve. I suspect the following should do the trick but it breaks 'Object required'
The table wanted is t2 (identified from the following behind the webpage)
Small sample of required result (will insert headings later)
Excel 2010
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]2[/TD]
[TD="align: right"]17.2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1011.3[/TD]
[TD="align: right"]0.4[/TD]
[TD="align: right"]10[/TD]
[TD="align: center"]3[/TD]
[TD="align: right"]17.2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1011.4[/TD]
[TD="align: right"]0.4[/TD]
[TD="align: right"]10[/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]17.6[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1011.5[/TD]
[TD="align: right"]0.4[/TD]
[TD="align: right"]10[/TD]
</tbody>
I've done a lot with VBA over the years but never used VBA to scrape data using IE or HTML.
I tried using a number of methods posted within the forum but every time I run into a problem I cannot solve. I suspect the following should do the trick but it breaks 'Object required'
Code:
Const sWebPage = "http://www.bom.gov.au/products/IDN60701/IDN60701.95929.shtml"
Sub Web_Table_Option_One()
Dim xml As Object
Dim html As Object
Dim objTable As Object
Dim result As String
Dim lRow As Long
Dim lngTable As Long
Dim lngRow As Long
Dim lngCol As Long
Dim ActRw As Long
Set xml = CreateObject("MSXML2.XMLHTTP.6.0")
With xml
.Open "GET", sWebPage, False
.send
End With
result = xml.responseText
Set html = CreateObject("htmlfile")
html.body.innerHTML = result
'Breaks on the line below Error 424 Object required. I think my problem is in Item and Tag Names
Set objTable = IE.Document.all.Item("{What should this be}").getElementsByTagName("{What should this be}") (0)
' Can't test the following yet
For lngTable = 0 To objTable.Length - 1
For lngRow = 0 To objTable(lngTable).Rows.Length - 1
For lngCol = 0 To objTable(lngTable).Rows(lngRow).Cells.Length - 1
ThisWorkbook.Sheets("Sheet1").Cells(ActRw + lngRow + 1, lngCol + 1) = objTable(lngTable).Rows(lngRow).Cells(lngCol).innerText
Next lngCol
Next lngRow
ActRw = ActRw + objTable(lngTable).Rows.Length + 1
Next lngTable
Set xml = Nothing
Set html = Nothing
Set objTable = Nothing
End Sub
The table wanted is t2 (identified from the following behind the webpage)
table id="t2" border="1" cellspacing="0" class="tabledata obs_table"
Small sample of required result (will insert headings later)
Excel 2010
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
01/11:30pm | CALM | - | - | Partly cloudy | - | - | - | - | |||||||
01/11:00pm | CALM | - | - | - | - | - | - | - | |||||||
01/10:30pm | CALM | - | - | - | - | - | - | - |
<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]2[/TD]
[TD="align: right"]17.2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1011.3[/TD]
[TD="align: right"]0.4[/TD]
[TD="align: right"]10[/TD]
[TD="align: center"]3[/TD]
[TD="align: right"]17.2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1011.4[/TD]
[TD="align: right"]0.4[/TD]
[TD="align: right"]10[/TD]
[TD="align: center"]4[/TD]
[TD="align: right"]17.6[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1011.5[/TD]
[TD="align: right"]0.4[/TD]
[TD="align: right"]10[/TD]
</tbody>
Sheet1
I am open to using Internet Explorer instead.