Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel For循环只运行两次,并且生成下标越界错误_Excel_Vba_Finance - Fatal编程技术网

Excel For循环只运行两次,并且生成下标越界错误

Excel For循环只运行两次,并且生成下标越界错误,excel,vba,finance,Excel,Vba,Finance,你好!我有一个关于我在VBA Excel中创建的程序的问题。 我编写了一个程序,从YAHOOFINANCE中提取金融票据,并将其发布在excel的新工作表上。但是,在导致此行下标超出范围错误之前,程序仅运行两次 ReDim results(1 To rows.Length, 1 To UBound(headers) + 1) 我很不确定为什么会发生这种情况,或者为什么程序可以运行两次,但不能再运行了。我已经检查了我的股票代码是否正确,并且雅虎财经上是否有股票 Option Explicit

你好!我有一个关于我在VBA Excel中创建的程序的问题。
我编写了一个程序,从YAHOOFINANCE中提取金融票据,并将其发布在excel的新工作表上。但是,在导致此行下标超出范围错误之前,程序仅运行两次

ReDim results(1 To rows.Length, 1 To UBound(headers) + 1)
我很不确定为什么会发生这种情况,或者为什么程序可以运行两次,但不能再运行了。我已经检查了我的股票代码是否正确,并且雅虎财经上是否有股票

Option Explicit


        Public Sub WriteOutFinancialInfo()
            Dim http As Object, s As String, x As String, y As String, z As String, w As String, i As Integer
        
            Set http = CreateObject("MSXML2.XMLHTTP")
          
            For i = 1 To 24
            
                y = CStr(Cells(1, i))
                
                
                
                
                
                x = "https://finance.yahoo.com/quote/"
                z = "/financials?p="
                w = x & y & z
                
                With http
                    .Open "GET", w, False
                    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
                    .send
                    s = .responseText
                End With
                
                Dim html As MSHTML.HTMLDocument, html2 As MSHTML.HTMLDocument, re As Object, matches As Object
                
                Set html = New MSHTML.HTMLDocument: Set html2 = New MSHTML.HTMLDocument
                Set re = CreateObject("VBScript.RegExp")
                
                html.body.innerHTML = s
                
                Dim headers(), rows As Object
                
                headers = Array("Breakdown", "TTM")
                Set rows = html.querySelectorAll(".fi-row")
                
                With re
                    .Global = True
                    .MultiLine = True
                    .Pattern = "\d{1,2}/\d{1,2}/\d{4}"
                    Set matches = .Execute(s)
                End With
                
                Dim results(), match As Object, r As Long, c As Long, startHeaderCount As Long
                startHeaderCount = UBound(headers)
                ReDim Preserve headers(0 To matches.Count + startHeaderCount)
            
                c = 1
                For Each match In matches
                    headers(startHeaderCount + c) = match
                    c = c + 1
                Next
                
                Dim row As Object
                ReDim results(1 To rows.Length, 1 To UBound(headers) + 1)
                
             
                For r = 0 To rows.Length - 1
                    html2.body.innerHTML = rows.Item(r).outerHTML
                    Set row = html2.querySelectorAll("[title],[data-test=fin-col]")
                    
                    For c = 0 To row.Length - 1
                        results(r + 1, c + 1) = row.Item(c).innerText
                    Next c
                Next
                
                Dim ws As Worksheet
                
                Set ws = ThisWorkbook.Worksheets("Sheet1")
                
                With ws
                    .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
                    .Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
                End With
                Sheets("Sheet1").Select
                Cells.Select
                Selection.Copy
                Sheets.Add After:=ActiveSheet
                ActiveSheet.Paste
                Range("J27").Select
            Next i
            
            
                
        End Sub

如果有人知道如何解决这个问题或者为什么会发生这种情况,我将非常感激。非常感谢:)

当失败时,
matches.Count+startHeaderCount
的值是多少?
startHeaderCount
=1
匹配。Count
=Nothing。也许这就是错误的来源。可能值得添加一个测试,看看您是否从regex
Execute