|
试下这一段代码,在我的电脑上是成功的。
另外,如果有多页,需要模拟点击,这个你搜一下吧。
Private Sub CommandButton1_Click()
On Error GoTo errexit:
'Dim IE As New InternetExplorer
Dim BoxAllData
Dim num As Object
Dim btn As Object
Dim tlb As Object, td As Object
Dim i As Long, StartRow As Long, startCOl As Long
Dim IE As Object 'As New InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
StartRow = 3 'excel 开始的行
startCOl = 1 'excel 开始的列
IE.Visible = True
IE.navigate "http://data.eastmoney.com/stock/tradedetail.html" '打开网址
Application.Wait Now + TimeSerial(0, 0, 3) '程序暂停3秒,等待loaded
Do While IE.readystate <> 4 'Wait till page is loaded
i = i + 1
If i > 30000 Then Exit Do '有时候会停顿,增加也给计数器,如果load的时间太长,就跳出,防止死循环
DoEvents
Loop
'TextBox1 = IE.document.Body.outerHTML '可以获得该文件的html代码
Dim tableX As Object, Row As Object, iRow As Long, iCOl As Long
Set tableX = IE.document.getElementsByTagName("table")(1) ' 如果不知道是哪一个表,就挨个尝试
'设置标题
Set Row = tableX.Rows(0) '第0行是标题
For iCOl = 0 To Row.Cells.Length - 1
'显示内容,注意在EXCEL里RANGE集合取表格位是从1开始,所以要加1
Cells(2, startCOl + iCOl) = Row.Cells(iCOl).innerText
Next
'表的值
For iRow = 1 To tableX.Rows.Length - 1 '第0行是标题,跳过
DoEvents
Excel.Application.StatusBar = iRow
Set Row = tableX.Rows(iRow)
'遍历每行所有表格
For iCOl = 0 To Row.Cells.Length - 1
'显示内容,注意在EXCEL里RANGE集合取表格位是从1开始,所以要加1
Cells(StartRow, startCOl + iCOl) = Row.Cells(iCOl).innerText
Next
StartRow = StartRow + 1
Next
'释放IE对象
IE.Quit
Set IE = Nothing
MsgBox "done"
Exit Sub
errexit:
MsgBox Err & ":" & Err.Description, vbCritical, "Error"
End Sub |
|