VBA:下标超出范围或类型不匹配

时间:2018-06-28 15:58:24

标签: excel vba excel-vba

对于VBA来说是新手,而且真的很卡住。下面是我的代码,您将在结尾看到Des和DesArr的For循环。我要做的就是从工作表“ SIC”中的工作表“ SIC”中拉出一列单元格,或者出现错误“下标超出范围”或“类型不匹配”,并且每当我尝试和谷歌/纠正一个,另一个错误取代它。如果有人可以帮助我完成这项工作,我将不胜感激!

Public Sub getGoogleDescriptions(strSearch As String)
    Dim URL As String, strResponse As String
    Dim objHTTP As Object
    Dim htmlDoc As HTMLDocument
    Dim result As String
    Dim i As Integer
    Dim u As Integer
    Dim resultArr As Variant
    Dim Des As String
    Dim DesArr(2 To 48) As Long


    Set htmlDoc = CreateObject("htmlfile")
    'Set htmlDoc = New HTMLDocument

    Dim objResults As Object
    Dim objResult As Object

    strSearch = Replace(strSearch, " ", "+")

    URL = "https://www.google.com/search?q=" & strSearch

    Set objHTTP = CreateObject("MSXML2.XMLHTTP")

    With objHTTP
        .Open "GET", URL, False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send
        htmlDoc.body.innerHTML = .responseText
    End With

    Set objResults = htmlDoc.getElementsByClassName("st")

    Debug.Print objResults(0).innerText

    result = CStr(objResults(0).innerText)
    resultArr = Split(result, " ", -1, 0)
    For i = LBound(resultArr) To UBound(resultArr) 'Define i to be the length of the List'
        Debug.Print i, resultArr(i) 'Prints the corresponding index value and array element'
    Next i 'repeat

    Set htmlDoc = Nothing
    Set objResults = Nothing
    Set objHTTP = Nothing


    Set wk = ActiveWorkbook

    For u = 2 To 48
        Des = Sheets("SIC").Range("C" & u).Value
        DesArr(u) = Des
    Next u

    Debug.Print DesArr(2)

End Sub

1 个答案:

答案 0 :(得分:3)

您会遇到类型不匹配的情况,因为它期望DesArr是一个长数据类型,该数据类型介于-2,147,483,648到2,147,483,647之间。

  1. 在子例程中使用时,它用作变体。因此,进行了2次更正-将其更改为如下所示的变体
  2. 然后将您的2到48调整为语句内...在这种情况下,它是2的简单偏移量,因此只需使用(u-2),并且Variant长度为47,从0开始而不是1

    Public Sub getGoogleDescriptions(strSearch As String)
    
        Dim URL As String, strResponse As String
        Dim objHTTP As Object
        Dim htmlDoc As HTMLDocument
        Dim result As String
        Dim i As Integer
        Dim u As Integer
        Dim resultArr As Variant
        Dim Des As String
        Dim DesArr(0) : ReDim DesArr(46)
    
        Set htmlDoc = CreateObject("htmlfile")
        'Set htmlDoc = New HTMLDocument
    
        Dim objResults As Object
        Dim objResult As Object
    
        strSearch = Replace(strSearch, " ", "+")
    
        URL = "https://www.google.com/search?q=" & strSearch
    
        Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    
        With objHTTP
            .Open "GET", URL, False
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .send
            htmlDoc.body.innerHTML = .responseText
        End With
    
        Set objResults = htmlDoc.getElementsByClassName("st")
    
        Debug.Print objResults(0).innerText
    
        result = CStr(objResults(0).innerText)
        resultArr = Split(result, " ", -1, 0)
        For i = LBound(resultArr) To UBound(resultArr) 'Define i to be the length of the List'
            Debug.Print i, resultArr(i) 'Prints the corresponding index value and array element'
        Next i 'repeat
    
        Set htmlDoc = Nothing
        Set objResults = Nothing
        Set objHTTP = Nothing
    
    
        Set wk = ActiveWorkbook
    
        For u = 2 To 48
            Des = Sheets("SIC").Range("C" & u).Value
            DesArr(u - 2) = Des
        Next u
    
        Debug.Print DesArr(0)
    

    结束子