Excel VBA - 从网站获取数据

Excel VBA - 从网站获取数据

问题描述:

我是usng followng方法从网站下载表格数据。但是每个循环迭代大约需要1.5秒。我需要加快速度。有什么建议吗?Excel VBA - 从网站获取数据

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 
+0

如果你调试它,哪一部分花费最多时间? – 2013-04-20 17:32:39

+0

请提供您想要获取数据的网站的链接。该方法根据网站而有所不同。 – Santosh 2013-04-20 17:32:53

+0

@SiddharthRout:GET请求部分。声明:打开“GET”,,错误 – Tejas 2013-04-20 18:04:26

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