我已经设置了一个宏来浏览唯一标识符列表,将这些标识符带到网站,然后将值返回到相邻的单元格中。我已经取得了一些成功的工作,但它已经非常间歇性,它也已经成为列表的一部分,然后打破了。我可以进入VBA,它将适用于整个列表,但是当我运行它时,宏将失败,通常使用
运行时错误'91':对象变量或未设置块变量
以下是我的代码,其中详细信息已更改为隐私:
Option Explicit
Sub Autofill()
Dim i As Integer
Dim FinalRow As Integer
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To FinalRow
Dim IE As New InternetExplorerMedium
IE.Visible = False
IE.navigate "https://website" & Cells(i, 1).Value & "last part of url"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document
Dim zDD As String
zDD = Trim(Doc.getElementById("NameValue").innerText)
Cells(i, 2).Value = zDD
Cells(i, 3).Value = Cells(i, 1).Value & "@company.com"
IE.Quit
'Application.Wait Now + #12:00:01 AM# "tried this with varying times to see if it would help, did not have any success"
Set IE = Nothing
Next i
End Sub
关于为什么它会逐步运行而不运行宏的任何想法?它可能与我使用网络浏览器有关吗?
答案 0 :(得分:0)
使用Cells
方法时,请指定您要引用的工作表。例如:
Worksheets("Sheet1").Cells(.....
如果你不使用它并从另一张表中调用宏,Excel会尝试在活动表中使用Cells
方法,你会收到错误。
答案 1 :(得分:0)
Doc.getElementById("NameValue")
需要更多时间 - 在Do Loop
返回对象之前有另一个Doc.getElementById("NameValue")
试试这个(未经测试):
Option Explicit
Public Sub AutoFillFromIE()
Dim ws As Worksheet, i As Long, IE As InternetExplorerMedium, docElement As Object
Set ws = ActiveSheet
Set IE = New InternetExplorerMedium
IE.Visible = False
For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
IE.Navigate "https://website" & ws.Cells(i, 1).Value2 & "last part of url"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
IE.Refresh
Do
Set docElement = IE.Document.getElementById("NameValue")
DoEvents
Loop Until Not docElement Is Nothing
ws.Cells(i, 2).Value2 = Trim$(docElement.innerText)
ws.Cells(i, 3).Value2 = ws.Cells(i, 1).Value2 & "@company.com"
Set docElement = Nothing
Next i
IE.Quit
End Sub