暱稱: joey0415
中學生
- 帖子
- 361
- 主題
- 57
- 精華
- 0
- 積分
- 426
- 點名
- 0
- 作業系統
- win7
- 軟體版本
- 2003,2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2010-5-13
- 最後登錄
- 2022-12-8
|
回復 5# a68951
table 4 其實是第五個,excel 是從0開始
不是沒有欄而是前面的欄位有合併與後面的talbe中的欄位長度不一置
下面是修改過的程式碼- Sub ex()
- Application.ScreenUpdating = False '停止 更新Sheet時 畫面,加速運算
- Application.DisplayAlerts = False '停止 刪除Sheet時 預設的警告
- Cells.Clear
- 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
-
- .Document.getElementById("date-field").Value = "104/08/12" '填入
-
- '---------,內建的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
- Set hTable = .Document.getElementsByTagName("table")(4) '第4個table
- For i = 0 To hTable.Rows.Length - 1 '前3個是標題與空白跳過
- For j = 0 To hTable.Rows(i).Cells.Length - 1
- If j = 0 And i > 2 Then
- Cells(i + 1, j + 1) = "'" & hTable.Rows(i).Cells(j).innerText
- Else
- Cells(i + 1, j + 1) = hTable.Rows(i).Cells(j).innerText
- End If
- Next
- Next
- .Quit
- End With
-
- Range("A3:B3").Insert Shift:=xlToRight
- Range("A2:B2").Select
- Selection.Cut
- Range("A3").Select
- ActiveSheet.Paste
- Range("E2:F2").Select
- Selection.Cut
- Range("O3").Select
- ActiveSheet.Paste
- Range("A1").Select
- Range("D2:h2").Insert Shift:=xlToRigh
-
- Application.ScreenUpdating = True '停止 更新Sheet時 畫面,加速運算
- Application.DisplayAlerts = True '停止 刪除Sheet時 預設的警告
-
- End Sub
複製代碼 |
|