运行时错误424对象需要VBA

时间:2017-01-12 21:03:53

标签: vba excel-vba excel

我尝试扫描网页时遇到此错误

' need to browse the web, I need a browser! So I create an Internet Explorer browser
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
Dim allRowOfDataG As Object

With appIE
    .Navigate "https://uk.investing.com/rates-bonds/portugal-government-bonds?maturity_from=70&maturity_to=260"
    .Visible = False
End With

Do While appIE.Busy
    DoEvents
Loop

'2y
Set allRowOfDataG = appIE.document.getElementById("pair_23784")

及其抛出错误 设置allRowOfDataG = appIE.document.getElementById(“pair_23784”)

1 个答案:

答案 0 :(得分:0)

我可以通过以下方式提取一些数据。也许您应该使appIE可见以进行问题排查。

Option Explicit

Sub SO41622944()
    On Error Resume Next
    ' need to browse the web, I need a browser! So I create an Internet Explorer browser
    Dim appIE As Object
    Dim allRowOfDataG As Object, i As Long, sText As String

    Set appIE = CreateObject("internetexplorer.application")
    If appIE Is Nothing Then Exit Sub
    With appIE
        .Navigate "https://uk.investing.com/rates-bonds/portugal-government-bonds?maturity_from=70&maturity_to=260"
        .Visible = False
        Do While .Busy
            DoEvents
        Loop
        '2y
        Set allRowOfDataG = .document.getElementById("pair_23784")
        If Not allRowOfDataG Is Nothing Then
            Debug.Print "Total items in allRowOfDataG: " & allRowOfDataG.all.Length
            For i = 0 To allRowOfDataG.all.Length - 1
                sText = Trim(allRowOfDataG.all(i).outerText)
                If Len(sText) > 0 Then Debug.Print i, sText ' Skip blank cells
            Next
            Set allRowOfDataG = Nothing
        End If
        .Quit
    End With
    Set appIE = Nothing
End Sub

输出

Total items in allRowOfDataG: 14
 2            Portugal 2-Year
 3            Portugal 2-Year
 5            0.258
 6            0.261
 7            0.311
 8            0.226
 9            -0.003
 10           -1.15%
 11           18:00:01