【问题标题】:Run-time error '9': Subscript out of range -Error运行时错误“9”:下标超出范围 - 错误
【发布时间】:2020-01-22 19:04:53
【问题描述】:

我在 excel 中运行 VBA 以进行一些网络抓取,但在尝试访问 Google Chrome 时出现此错误。如何避免这种情况?我将点击调试,然后一行会突出显示...

这是弹出错误的地方

这是必须给出问题的突出显示的代码行

Full Code in this picture

完整代码如下:

Private Sub time_sheet_filling()

    Dim I As Long
    Dim IE As Object
    Dim doc As Object
    Dim objElement As Object
    Dim objCollection As Object

    ' Create InternetExplorer Object
    Set IE = CreateObject("ChromeTab.ChromeFrame")
    IE.Visible = True

    ' Send the form data To URL As POST binary request
    IE.navigate "https://wistar.dispondo.net/#/login"

    ' Wait while IE loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

        'Load the logon page
    Set objCollection = IE.Document.getElementsByTagName("input")
    I = 0
    While I < objCollection.Length
        If objCollection(I).Name = "username" Then
            ' Set text to enter
            objCollection(I).Value = "MyUsername"
        End If
        If objCollection(I).Name = "password" Then
            ' Set text for password
            objCollection(I).Value = "MyPasword"
        End If
        If objCollection(I).Type = "submit" And objCollection(I).Name = "btnSubmit" Then ' submit button clicking
            Set objElement = objCollection(I)
        End If
        I = I + 1
    Wend

    objElement.Click    ' click button to load the form

    ' Wait while IE re-loading...
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop

    ' Show IE
    IE.Visible = True
Dim links, link
Dim n, j
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
For j = 0 To n - 1 Step 2
    links(j).Click
'I have some operations to be done will post another question for this

Next



End Sub

【问题讨论】:

  • 提前感谢您的帮助!!
  • 图像中的代码 文本中的代码! Debug.print Chromepath &amp; Sheets...an 在chrome.exe" 之后看到缺少的空白(除非单元格中有一个(令人惊讶的)起始空白)。

标签: excel vba google-chrome runtime-error


【解决方案1】:

如果在代码运行时活动工作簿中没有名为“网站数据”的工作表,则会发生该错误。工作表名称不匹配,或者代码运行时另一个工作簿处于活动状态。

确保工作表名称正确,并明确引用它所在的工作簿(这样代码运行时哪个工作簿处于活动状态并不重要)。如果“网站数据”表在运行代码的工作簿中,则使用以下方法引用单元格中的值:

ThisWorkbook.Sheets("Website Data").Cells(1,1).Value

【讨论】:

    【解决方案2】:

    您不能以这种方式使用 Chrome 代替 IE。 Chrome 只能通过 Selenium 使用 VBA 进行网页抓取。 IE 是唯一具有 COM 接口的浏览器,需要通过 VBA 控制应用程序:

    更多信息:

    About the Component Object Model (COM)

    About Selenium browser automation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多