我需要每隔 5 秒从 www.dsebd.org 提取数据。此 Vba 代码提取数据但不会自动运行。请帮帮我。
Sub ButtonCode()
' execute macros
Call GetCotton
' submit macro to run again in 5 sec
Application.OnTime Now + TimeValue("00:00:05"), "ButtonCode"
End Sub
Sub GetCotton()
Dim xml As Object
Dim html As Object
Dim elemcollection As Object
Dim result As String
Dim t As Long, r As Long, c As Long, ActRw As Long
Set xml = CreateObject("MSXML2.XMLHTTP.6.0")
With xml
.Open "GET", "http://www.dsebd.org/dseX_share.php", False
.send
End With
result = xml.responseText
Set html = CreateObject("htmlfile")
html.body.innerHTML = result
Set elemcollection = html.getElementsByTagName("table")
For t = 0 To elemcollection.Length - 1
For r = 0 To elemcollection(t).Rows.Length - 1
For c = 0 To elemcollection(t).Rows(r).Cells.Length - 1
ThisWorkbook.Sheets("Sheet1").Cells(ActRw + r + 1, c + 1) = elemcollection(t).Rows(r).Cells(c).innerText
Next c
Next r
ActRw = ActRw + elemcollection(t).Rows.Length + 1
Next t
End Sub
答案1
您没有说明错误消息是什么,也没有说明错误发生的位置。我怀疑它找不到相关代码。因此,更改
Application.OnTime Now + TimeValue("00:00:05"), "ButtonCode"
到
Application.OnTime Now + TimeValue("00:00:05"), "thisworkbook.ButtonCode"