我的代码有问题,它会循环几次然后出错:
Debug.Print IEElements.Item.innerText
错误是:
<块引用>运行时错误“91”:对象变量或未设置块变量
我已经阅读了有关此错误的文档,但我仍然不明白为什么它会间歇性地出现。
完整代码如下:
Sub InternetExplorerObject()
Dim IEObject As InternetExplorer
'Create a new instance of the Internet Explorer Object
Set IEObject = New InternetExplorer
'Switch to see IE window appear during scraping
IEObject.Visible = True
Dim c As Range
Dim fee As Object
rw = 2
For Each c In ActiveSheet.Range("A2", ActiveSheet.Cells(Rows.Count, "A").End(xlUp))
'Navigate to a URL we specify.
IEObject.Navigate Url:="https://www.fundinfo.com/en/LU-prof/LandingPage?query=" & c & "#tab=1" ', Flags:=navOpenInNewWindow
'This Loop will keep us waiting as long as the IEObject is in a Busy state or
'the ReadyState does not communicate complete.
Do While IEObject.Busy = True Or IEObject.ReadyState <> READYSTATE_COMPLETE
'Wait one second, and then try again
Application.Wait Now + TimeValue("00:00:01")
Loop
'Print the URL we are currently at and row number.
Debug.Print IEObject.LocationURL
Debug.Print rw
'Get the HTML document for the page
Dim IEDocument As HTMLDocument
Set IEDocument = IEObject.Document
'Grab a elements collection
Dim IEElements As IHTMLElementCollection
Set IEElements = IEDocument.getElementsByClassName("member-only OFST452200")
'skip if ISIN doesn't exist (i.e. tag doesn't exist in page)
On Error Resume Next
Debug.Print IEElements.Item.innerText
ActiveSheet.Cells(rw, 2).Value = IEElements.Item.innerText
IEObject.Quit
Set IEObject = Nothing
rw = rw + 1
Next
End Sub
Screenshot of Excel table 其中第一列是 ISIN,第二列是产生费用。
有什么想法吗?
编辑:谢谢大家。我会详细了解错误处理。我刚刚实施了@Vincent G 的建议。 我已经更新了代码,现在我遇到的问题是我无法关闭 IE 的实例。我尝试添加以下代码来杀死 IE:
IEObject.Quit
Set IEObject = Nothing
Stack Overflow 的新手,让我知道是否应该将此标记为“已解决”并开始一个新线程。