簡體   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