With CreateObject("internetexplorer.application")
.Visible = True
.Navigate "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php"
Do Until .ReadyState = 4
DoEvents
Loop
'---------,內建的fireevent 的onchange失效,改用調用js的方法--------
Set evt = .Document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
Set lst = .Document.all("selectType") 'option的name??selectType,但getElementsByName無法處理,要all才行
lst.selectedIndex = 1
lst.dispatchEvent evt
.Document.all("query-button").Click
Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop
Application.Wait Now + TimeValue("00:00:5")
' Stop
'For Z = 0 To 10
'MsgBox .Document.getElementsByTagName("table")(Z).innerText
'Next
' Stop
Set hTable = .Document.getElementsByTagName("table")(4) '第4個table
tt = hTable.Rows.Length
qq = hTable.Rows(2).Cells.Length
With ActiveSheet
For i = 3 To hTable.Rows.Length - 1 '前3個??標題與空白跳過
For j = 0 To hTable.Rows(i).Cells.Length - 1
.Cells(i - 2, j + 1) = hTable.Rows(i).Cells(j).innerText
Next
Next
End With