【发布时间】:2016-05-27 15:55:21
【问题描述】:
我正在尝试制作一个电子表格,从网站中提取当前价格并记录下来。我在这一行收到“对象变量或未设置块变量”错误:
costForGame = appIE.document.getElementById("used_price").getElementsByTagName("span")(0).innerText 'Finds "used_price" and then any span within it and returns the first value
我不确定我的问题是什么,或者如何解决它。想法?
Sub ImportUsedPrice()
Dim Console As String
Dim GameTitle As String
Dim URL As String
Dim costForGame As Double
Range("G3").Clear 'clears D3
Range("H3").Clear
Range("J3").Clear
Range("K3").Clear
'creates web browser
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
Dim indexB As Single
Dim indexC As Single
Dim nameCell As String
Dim consoleCell As String
indexB = 3
indexC = 3
nameCell = "B" & indexB
consoleCell = "C" & indexC
While IsEmpty(nameCell) = False And IsEmpty(consoleCell) = False
Range("J3").Value = IsEmpty(nameCell) 'used for debugging
Range("K3").Value = IsEmpty(consoleCell) 'used for debugging
Range("G3").Value = indexB 'used for debugging
Range("H3").Value = indexC 'used for debugging
'access the webpage
Console = Range(consoleCell).Value
GameTitle = Range(nameCell).Value
URL = "http://www.pricecharting.com/game/" & Console & "/" & GameTitle 'makes complete URL
With appIE
.Navigate URL
.Visible = True 'True shows the browser, False does not
End With
'wait for the page to load
Do While appIE.Busy
DoEvents
Loop
'pull the data
costForGame = appIE.document.getElementById("used_price").getElementsByTagName("span")(0).innerText 'Finds "used_price" and then any span within it and returns the first value
Range("D3").Value = costForGame 'records price in D3
indexB = indexB + 1
indexC = indexC + 1
Wend
appIE.Quit 'Closes Internet Explorer
Set appIE = Nothing 'Release memory for IE
Range("D3").Select 'selects cell D3
Range(Selection, Selection.End(xlDown)).Select 'selects cell D3 and all cells below it
Selection.NumberFormat = "$#,##0.00" 'formats all selected cells to dollars
End Sub
【问题讨论】:
-
在我头上,但你创建
Set appIE = CreateObject("internetexplorer.application")却从未设置对象?