New_Grasshopper
New Member
- Joined
- Jan 3, 2022
- Messages
- 2
- Office Version
- 2016
- Platform
- Windows
I am trying to automate copying multiple tables of data from a web page to excel and found a soultion that worked for another user:
Trying to paste data from a website into excel into correct format (kudos to SundanceKid and Kokosek)
However when I try this for the website I am trying to copy from I get a Run-time error "91": Object variable or with Block variable not set when it reaches the step "For Each tr in Htable"....
When I check the locals window it appears that the hTable object is empty (it works fine when I try to replicate the datacapture from the webpage in the source post. The only changes to the original code is the web page string and the table name, I know the webpage string is correct because it loads when I paste the string into a browser but the site requires a log in so maybe this method wont work ? Alternatively I might have the table name wrong but I don't see how?
I have attached a pic of the target webpage showing the sourcecode and when I hover over the highligted line the table in the top window also gets highlighted.
My version of the code is:
Public Sub GetTable()
Dim html As MSHTML.HTMLDocument, hTable As Object, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set html = New MSHTML.HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "Search results", False
.send
html.body.innerHTML = .responseText
End With
Set hTable = html.getElementsByClassName("table table-striped sticky-enabled tableheader-processed sticky-table")(0)
Dim td As Object, tr As Object, th As Object, r As Long, c As Long, li As Object
For Each tr In hTable.getElementsByTagName("tr")
r = r + 1: c = 1
For Each th In tr.getElementsByTagName("th")
ws.Cells(r, c) = th.innerText
c = c + 1
Next
For Each td In tr.getElementsByTagName("td")
ball = 0
For Each li In td.getElementsByTagName("li")
ws.Cells(r, c + ball) = li.innerText
ball = ball + 1
Next
c = c + 1
Next
Next
End Sub
Trying to paste data from a website into excel into correct format (kudos to SundanceKid and Kokosek)
However when I try this for the website I am trying to copy from I get a Run-time error "91": Object variable or with Block variable not set when it reaches the step "For Each tr in Htable"....
When I check the locals window it appears that the hTable object is empty (it works fine when I try to replicate the datacapture from the webpage in the source post. The only changes to the original code is the web page string and the table name, I know the webpage string is correct because it loads when I paste the string into a browser but the site requires a log in so maybe this method wont work ? Alternatively I might have the table name wrong but I don't see how?
I have attached a pic of the target webpage showing the sourcecode and when I hover over the highligted line the table in the top window also gets highlighted.
My version of the code is:
Public Sub GetTable()
Dim html As MSHTML.HTMLDocument, hTable As Object, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set html = New MSHTML.HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "Search results", False
.send
html.body.innerHTML = .responseText
End With
Set hTable = html.getElementsByClassName("table table-striped sticky-enabled tableheader-processed sticky-table")(0)
Dim td As Object, tr As Object, th As Object, r As Long, c As Long, li As Object
For Each tr In hTable.getElementsByTagName("tr")
r = r + 1: c = 1
For Each th In tr.getElementsByTagName("th")
ws.Cells(r, c) = th.innerText
c = c + 1
Next
For Each td In tr.getElementsByTagName("td")
ball = 0
For Each li In td.getElementsByTagName("li")
ws.Cells(r, c + ball) = li.innerText
ball = ball + 1
Next
c = c + 1
Next
Next
End Sub