返回列表 上一主題 發帖

[發問] 有關交易所融資券餘額資料查詢網頁

回復 1# a68951
此為ie點擊法,秒數請自行修改
日期請自設為變數即可
  1. Sub ex()

  2.     With CreateObject("internetexplorer.application")
  3.         .Visible = True
  4.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php"
  5.         Do Until .ReadyState = 4
  6.             DoEvents
  7.         Loop
  8.         
  9.         .Document.getElementById("date-field").Value = "104/08/12" '填入
  10.          
  11.         '---------,內建的fireevent 的onchange失效,改用調用js的方法--------
  12.         Set evt = .Document.createEvent("HTMLEvents")
  13.         evt.initEvent "change", True, False
  14.         Set lst = .Document.all("selectType") 'option的name是selectType,但getElementsByName無法處理,要all才行
  15.         lst.selectedIndex = 1
  16.         lst.dispatchEvent evt
  17.    
  18.         .Document.all("query-button").Click


  19.          Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop

  20.         Application.Wait Now + TimeValue("00:00:5")
  21.         Stop
  22.         
  23.         'For Z = 0 To 10
  24.         'MsgBox .Document.getElementsByTagName("table")(Z).innerText
  25.         'Next
  26.             Stop
  27.         Set hTable = .Document.getElementsByTagName("table")(4) '第4個table
  28.         tt = hTable.Rows.Length
  29.         qq = hTable.Rows(2).Cells.Length

  30.             With ActiveSheet
  31.                 For i = 3 To hTable.Rows.Length - 1 '前3個是標題與空白跳過
  32.                     For j = 0 To hTable.Rows(i).Cells.Length - 1
  33.                         .Cells(i - 2, j + 1) = hTable.Rows(i).Cells(j).innerText
  34.                     Next
  35.                 Next
  36.             End With

  37.         .Quit
  38. '        MsgBox "OK"
  39.     End With
  40.    
  41. End Sub
複製代碼

TOP

回復 5# a68951

table 4  其實是第五個,excel 是從0開始



不是沒有欄而是前面的欄位有合併與後面的talbe中的欄位長度不一置

下面是修改過的程式碼
  1. Sub ex()
  2.     Application.ScreenUpdating = False  '停止 更新Sheet時 畫面,加速運算
  3.     Application.DisplayAlerts = False  '停止 刪除Sheet時 預設的警告
  4.     Cells.Clear
  5.     With CreateObject("internetexplorer.application")
  6.         .Visible = True
  7.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php"
  8.         Do Until .ReadyState = 4
  9.             DoEvents
  10.         Loop
  11.         
  12.         .Document.getElementById("date-field").Value = "104/08/12" '填入
  13.          
  14.         '---------,內建的fireevent 的onchange失效,改用調用js的方法--------
  15.         Set evt = .Document.createEvent("HTMLEvents")
  16.         evt.initEvent "change", True, False
  17.         Set lst = .Document.all("selectType") 'option的name是selectType,但getElementsByName無法處理,要all才行
  18.         lst.selectedIndex = 1
  19.         lst.dispatchEvent evt
  20.    
  21.         .Document.all("query-button").Click


  22.          Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop

  23.         Application.Wait Now + TimeValue("00:00:5")
  24. '        Stop
  25.         
  26.         'For Z = 0 To 10
  27.         'MsgBox .Document.getElementsByTagName("table")(Z).innerText
  28.         'Next
  29.         Set hTable = .Document.getElementsByTagName("table")(4) '第4個table


  30.                 For i = 0 To hTable.Rows.Length - 1 '前3個是標題與空白跳過
  31.                     For j = 0 To hTable.Rows(i).Cells.Length - 1
  32.                         If j = 0 And i > 2 Then

  33.                                 Cells(i + 1, j + 1) = "'" & hTable.Rows(i).Cells(j).innerText

  34.                             Else
  35.                                 Cells(i + 1, j + 1) = hTable.Rows(i).Cells(j).innerText
  36.                         End If
  37.                     Next
  38.                 Next


  39.         .Quit
  40.     End With
  41.    
  42.     Range("A3:B3").Insert Shift:=xlToRight
  43.     Range("A2:B2").Select
  44.     Selection.Cut
  45.     Range("A3").Select
  46.     ActiveSheet.Paste
  47.     Range("E2:F2").Select
  48.     Selection.Cut
  49.     Range("O3").Select
  50.     ActiveSheet.Paste
  51.     Range("A1").Select
  52.     Range("D2:h2").Insert Shift:=xlToRigh
  53.    
  54.         Application.ScreenUpdating = True  '停止 更新Sheet時 畫面,加速運算
  55.         Application.DisplayAlerts = True '停止 刪除Sheet時 預設的警告
  56.    
  57. End Sub
複製代碼

TOP

        靜思自在 : 願要大、志要堅、氣要柔、心要細。
返回列表 上一主題