│題目│
鉅亨基金網每一檔基金的歷史淨值都有一個網頁,且淨值資料分別放在三個表格。試撰寫程式用QueryTable物件下載摩根東協基金(JP東協)最新資料的那個表格內容到「臨時資料」工作表。該網頁網址如下:
http://fund.cnyes.com/detail/摩根東協基金/B08,017/historical/
提示:請用WebTables屬性接收表格的編號。
│解答│
1.找出表格代號 *執行前開啟即時運算視窗檢視
Sub HTMLtablebb()
Dim myIE As Object
Dim myTable
Dim Tables
Dim i As Integer
Dim j As Integer
Set myIE = CreateObject("InternetExplorer.Application")
With myIE
'.Visible = True
.navigate "http://fund.cnyes.com/detail/摩根東協基金/B08,017/historical/"
Do Until .readyState = 4
Loop
Set Tables = .document.getElementsByTagName("table")
End With
For Each myTable In Tables
i = i + 1
If i < 8 Then
Debug.Print myTable.innerText
Debug.Print i
Debug.Print "----------"
End If
Next
myIE.Quit
Set myIE = Nothing
End Sub
2.下載表格
Sub 使用QueryTable下載摩根東協基金()
Dim QryTbl As QueryTable
With Worksheets("臨時資料")
.Cells.Clear
Set QryTbl = .QueryTables.Add("URL;http://fund.cnyes.com/detail/摩根東協基金/B08,017/historical/", .Range("A1"))
End With
With QryTbl
.WebTables = "6"
.Refresh False
End With
End Sub