I'm trying to pull data from multiple webpages (different stock pages from the same site). I can get the data pulled for the first 3 times the loop is executed but on the 4th iteration it brings up error 91: Object Variable or With block Variable not set up.
I tried moving around the internet explorer opening command so that it opens a new browser at the beginning of each iteration, and closes it at the end of the loop, to make sure the IE object wasn't somehow failing. That didn't work, same issue.
Sub GetStock()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim cellnum As Range: Set cellnum = Range(ActiveCell.Address)
Dim i As Integer
Dim IE As Object
Dim text As String
i = 1
Do Until i > 10
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
cellnum = Range(ActiveCell.Offset(i, 7).Address)
With IE
.navigate cellnum.Value
Do While .Busy And .readyState <> 4: DoEvents: Loop
Sleep 1000
text = .Document.getElementsByClassName("classname")(1).outerText
End With
ws.Cells(i, 12).Value = text
i = i + 1
IE.Quit
Loop
End Sub
The links to the webpage are held within cells, hence the cellnum code. Finds the correct cell, retrieves the webpage within it, then moves on to the cell below it. The code is working perfectly for the first 3 iterations but for some reason fails on the 4th. The error code identifies the "text=.document.getElementsByClassName..." line as the error.