我一直在研究这个VBA代码。目标是从我的excel文档中复制一些东西,在线搜索它,然后从html代码中取回一些东西。我一直收到一个错误代码,上面写着" Object Required"有时它表示"对象变量或With块变量未设置。"这一切都集中在线上" set elementTWO = elementONE.Item(i).innerText
我尝试删除" Set"我已经尝试将elementTWO更改为字符串。另一件非常奇怪的事情是For ... Next Loop不会让我这么做。#34;退出。"它返回一个错误。我尝试过其他一些事情但无济于事。非常感谢任何帮助
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
答案 0 :(得分:0)
我认为这就是你想要的,它处理循环但在此期间失败。你可以至少调试它,看看为什么:
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