我对这个对象所需的错误感到困惑 VBA
I am confused by this object required error VBA
我已经研究这个 VBA 代码很长时间了。目标是从我的 excel 文档中复制一些内容,在线搜索它,然后从 html 代码中提取一些内容。我不断收到一个错误代码,显示 "Object Required",有时它显示 "Object variable or With block variable not set." 它全部集中在行“set elementTWO = elementONE.Item(i).innerText
我试过删除单词 "Set" 我试过将 elementTWO 更改为字符串。另一个非常奇怪的部分是 For...Next 循环不会让我 "exit for." 它 returns 出错。我已经尝试了一些其他的东西无济于事。非常感谢任何帮助
Option Explicit
Option Compare Text
Public Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub GetCategory()
Dim RowNo, ColNo, i As Integer
Dim Parent, Item, URL1, URL2, URL3 As String
Dim objHTML As Object
Dim elementONE As Object
Dim elementTWO As String
RowNo = 3
ColNo = 5
URL1 = "http://www.infores.com/public/us/knowledgegroup/resources/resources.pli?defaultDataType=&pageid=validatorresults&upc1="
URL2 = "&upc2="
URL3 = "&submitupc=find+it%21"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With Worksheets(1)
While RowNo <= 5
Parent = Cells(RowNo, ColNo)
Item = Cells(RowNo, ColNo + 1)
With ie
.navigate URL1 & Parent & URL2 & Item & URL3
.Visible = False
'Delay while IE loads
Do While (ie.Busy Or ie.READYSTATE <> READYSTATE.READYSTATE_COMPLETE)
DoEvents
Loop
'Put html code in document object
Set objHTML = .document
DoEvents
End With
Set elementONE = objHTML.getElementsByTagName("TD") 'Break Down HTML code
For i = 1 To elementONE.Length
elementTWO = elementONE.Item(i).innerText
If elementTWO = "Description" Then 'Find the Category
Cells(RowNo, ColNo + 4) = elementONE.Item(i + 1).innerText 'Put Category into excel
End If
Next i
DoEvents
ie.Quit
RowNo = RowNo + 1
Wend
End With
End Sub
我认为这就是您想要的,它处理循环但随后失败。您至少可以调试它并查看原因:
Option Explicit
Option Compare Text
Public Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub GetCategory()
Dim RowNo, ColNo, i As Integer
Dim Parent, Item, URL1, URL2, URL3 As String
Dim objHTML As Object, elementONE As Object, elementTWO As String
RowNo = 3
ColNo = 5
URL1 = "http://www.infores.com/public/us/knowledgegroup/resources/resources.pli?defaultDataType=&pageid=validatorresults&upc1="
URL2 = "&upc2="
URL3 = "&submitupc=find+it%21"
Dim ie As Object
'Set ie = CreateObject("InternetExplorer.Application")
With Worksheets(1)
While RowNo <= 5
Parent = Cells(RowNo, ColNo)
Item = Cells(RowNo, ColNo + 1)
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate URL1 & Parent & URL2 & Item & URL3
.Visible = False
'Delay while IE loads
Do While (ie.Busy Or ie.READYSTATE <> READYSTATE.READYSTATE_COMPLETE)
DoEvents
Loop
'Put html code in document object
Set objHTML = .document
DoEvents
End With
Set elementONE = objHTML.getElementsByTagName("TD") 'Break Down HTML code
For i = 0 To elementONE.Length - 1
elementTWO = elementONE(i).innerText
If elementTWO = "Description" Then 'Find the Category
Cells(RowNo, ColNo + 4) = elementONE(i + 1).innerText 'Put Category into excel
End If
Next i
DoEvents
ie.Quit
RowNo = RowNo + 1
Wend
End With
End Sub
我已经研究这个 VBA 代码很长时间了。目标是从我的 excel 文档中复制一些内容,在线搜索它,然后从 html 代码中提取一些内容。我不断收到一个错误代码,显示 "Object Required",有时它显示 "Object variable or With block variable not set." 它全部集中在行“set elementTWO = elementONE.Item(i).innerText
我试过删除单词 "Set" 我试过将 elementTWO 更改为字符串。另一个非常奇怪的部分是 For...Next 循环不会让我 "exit for." 它 returns 出错。我已经尝试了一些其他的东西无济于事。非常感谢任何帮助
Option Explicit
Option Compare Text
Public Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub GetCategory()
Dim RowNo, ColNo, i As Integer
Dim Parent, Item, URL1, URL2, URL3 As String
Dim objHTML As Object
Dim elementONE As Object
Dim elementTWO As String
RowNo = 3
ColNo = 5
URL1 = "http://www.infores.com/public/us/knowledgegroup/resources/resources.pli?defaultDataType=&pageid=validatorresults&upc1="
URL2 = "&upc2="
URL3 = "&submitupc=find+it%21"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With Worksheets(1)
While RowNo <= 5
Parent = Cells(RowNo, ColNo)
Item = Cells(RowNo, ColNo + 1)
With ie
.navigate URL1 & Parent & URL2 & Item & URL3
.Visible = False
'Delay while IE loads
Do While (ie.Busy Or ie.READYSTATE <> READYSTATE.READYSTATE_COMPLETE)
DoEvents
Loop
'Put html code in document object
Set objHTML = .document
DoEvents
End With
Set elementONE = objHTML.getElementsByTagName("TD") 'Break Down HTML code
For i = 1 To elementONE.Length
elementTWO = elementONE.Item(i).innerText
If elementTWO = "Description" Then 'Find the Category
Cells(RowNo, ColNo + 4) = elementONE.Item(i + 1).innerText 'Put Category into excel
End If
Next i
DoEvents
ie.Quit
RowNo = RowNo + 1
Wend
End With
End Sub
我认为这就是您想要的,它处理循环但随后失败。您至少可以调试它并查看原因:
Option Explicit
Option Compare Text
Public Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum
Sub GetCategory()
Dim RowNo, ColNo, i As Integer
Dim Parent, Item, URL1, URL2, URL3 As String
Dim objHTML As Object, elementONE As Object, elementTWO As String
RowNo = 3
ColNo = 5
URL1 = "http://www.infores.com/public/us/knowledgegroup/resources/resources.pli?defaultDataType=&pageid=validatorresults&upc1="
URL2 = "&upc2="
URL3 = "&submitupc=find+it%21"
Dim ie As Object
'Set ie = CreateObject("InternetExplorer.Application")
With Worksheets(1)
While RowNo <= 5
Parent = Cells(RowNo, ColNo)
Item = Cells(RowNo, ColNo + 1)
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate URL1 & Parent & URL2 & Item & URL3
.Visible = False
'Delay while IE loads
Do While (ie.Busy Or ie.READYSTATE <> READYSTATE.READYSTATE_COMPLETE)
DoEvents
Loop
'Put html code in document object
Set objHTML = .document
DoEvents
End With
Set elementONE = objHTML.getElementsByTagName("TD") 'Break Down HTML code
For i = 0 To elementONE.Length - 1
elementTWO = elementONE(i).innerText
If elementTWO = "Description" Then 'Find the Category
Cells(RowNo, ColNo + 4) = elementONE(i + 1).innerText 'Put Category into excel
End If
Next i
DoEvents
ie.Quit
RowNo = RowNo + 1
Wend
End With
End Sub