I have a code that is supposed to open a website, select a location, copy the HTML table to the Excel sheet and repeat on another location. However when I tried running the 'For' loop, I got error at the 4th iteration. The message said "Object variable or With block variable not set". The debug tool pointed at line 44
Sub ParseTable()
Dim IE As InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument 'Document object
Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
Dim eleRow As MSHTML.IHTMLElement 'Row elements
Dim eleCol As MSHTML.IHTMLElement 'Column elements
Dim ieURL As String 'URL
Dim x As Integer
Dim y As String
y = "A1"
For x = 1 To 4
If x <> 2 Then 'Skip iteration 2
Set IE = New InternetExplorer
IE.Visible = True
Application.ScreenUpdating = False
ieURL = "***"
IE.navigate ieURL
'Wait
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set htmldoc = IE.document 'Document webpage
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
IE.document.getElementById("ddlLevel1").selectedIndex = x
IE.document.getElementById("ddlLevel1").FireEvent ("onchange")
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set eleColtr = IE.document.getElementsByTagName("tr") 'Find all tr tags
'This section populates Excel
i = 0 'start with first value in tr collection
For Each eleRow In eleColtr 'for each element in the tr collection
Set eleColtd = IE.document.getElementsByTagName("tr")(i).getElementsByTagName("td") 'get all the td elements in that specific tr
j = 0 'start with the first value in the td collection
For Each eleCol In eleColtd 'for each element in the td collection
Sheets("Sheet1").Range(y).Offset(i, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
j = j + 1 'move to next element in td collection
Next eleCol 'rinse and repeat
i = i + 1 'move to next element in td collection
Next eleRow 'rinse and repeat
Sheets("Sheet1").Range(y).Offset(i, 0).Select
y = "A" & ActiveCell.Row
IE.Quit
End If
Next x
Application.ScreenUpdating = True
End Sub
Not sure what could be the possible cause. I did get the tables from first 4 locations (minus location 2) on my Excel sheet. Pardon for my long, inefficient code (I am not a programmer myself). The webpage that I use requires login and has confidential data, but I will try to provide input as much as possible. Thanks in advance for any help!