【问题标题】:EXCEL - object variable or with block variable not setEXCEL - 对象变量或未设置块变量
【发布时间】: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") 却从未设置对象?

标签: excel vba


【解决方案1】:

改变这两行:

Do While appIE.Busy

Do Until appIE.ReadyState = 4

还有

costForGame = appIE.document.getElementById("used_price").getElementsByTagName("span")(0).innerText

costForGame = Replace(appIE.document.getElementById("used_price").Children(1).innerText, "$", "")

编辑

首先Children(1) 必须是Children(0)。但即使使用 IE11,在站点完全加载之前,appIE.Busy 也会为 false。我的版本是 11.306.10586.0 和更新版本 11.0.31 和 appIE.ReadyState 就像一个魅力。这里是我用来测试你的子的完整代码。 (运行没有错误)

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
    URL = "https://www.pricecharting.com/game/playstation-3/grand-theft-auto-v"

    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
    Do Until appIE.ReadyState = 4
        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
    costForGame = Replace(appIE.document.getElementById("used_price").Children(0).innerText, "$", "")
    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

【讨论】:

  • 我进行了这些更改,现在收到一条错误消息,提示“对象'IWebBrowser2'的方法'文档'失败”,调试行是 costForGame = Replace(appIE.document.getElementById("used_price" ).Children(1).innerText, "$", "") 我还查看了您建议的 ReadyState 行,发现 IE 11 不再支持该行。这可能是我的问题的一部分吗?
  • 请查看我编辑的答案并检查是否有帮助或导致其他错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
相关资源
最近更新 更多