[英]Excel VBA - Fetch Data from Website
我使用以下方法從網站下載表格數據 。 但每次循環迭代需要大約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
您應該考慮只Open
一次MSXML2.XMLHTTP對象,然后在每次循環迭代中調用Open
和Send
。 您可以在開始循環之前將其存儲在變量中。 我確信大部分時間都花在創建對象上,而不是在實際的Open
調用中。 根據您在上面的代碼中調用它的方式,您可以在每個循環迭代中創建一個新對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.