繁体   English   中英

运行时错误“9”:下标超出范围 -Error

[英]Run-time error '9': Subscript out of range -Error

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

这是弹出错误的地方

这是弹出错误的地方

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

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

这张图片中的完整代码

完整代码如下:

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

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

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

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

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

想要查询更多的信息:

关于组件对象模型 (COM)

关于 Selenium 浏览器自动化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM