3

我正在使用以下方法从站点下载表格数据。但是每次循环迭代大约需要 1.5 秒。我需要加快速度。请问有什么建议吗?

Sub GetData_Sai()
    Dim htm As Object
    Dim x As Long, y As Long

    Set htm = CreateObject("htmlFile")
    Application.ScreenUpdating = False

    Row = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 1
    For p = Range("F1") To Range("F1") + 200
        Debug.Print p
        Set htm = CreateObject("htmlFile")
        With CreateObject("msxml2.xmlhttp")
            .Open "GET", <site_url_goes_here>, False
            .send
            htm.body.innerhtml = .responsetext
        End With

        With htm.getelementbyid("item")
            Sheet2.Cells(Row, 4).Value = p
            For x = 1 To .Rows.Length - 1
                For y = 0 To .Rows(x).Cells.Length - 1
                    Sheet2.Cells(Row, y + 1).Value = .Rows(x).Cells(y).innertext
                Next y
                Row = Row + 1
            Next x
        End With
        Set htm = Nothing

        If p Mod 10 = 0 Then ThisWorkbook.Save
    Next

    Range("F1") = p

    Application.ScreenUpdating = True
    MsgBox "Done", vbInformation
End Sub
4

1 回答 1

0

您应该考虑只打开一次 MSXML2.XMLHTTP 对象,然后在每次循环迭代中调用Openand 。Send您可以在开始循环之前将其存储在变量中。我确信大部分时间都花在创建对象而不是实际Open调用中。根据您在上面的代码中调用它的方式,您在每次循环迭代中创建一个新对象。

于 2013-10-02T22:56:27.853 回答