返回列表 上一主題 發帖

[發問] 要如何下載網頁中的表格資料

回復 1# jasonwu0114
  1. Sub 網銀()
  2.     Dim URL As String, shts As Worksheet
  3.     Dim x As Variant, xi As Integer, A As Object, xlHtm
  4.     Set shts = ActiveSheet '  '("工作表2")
  5.     shts.Cells.Clear
  6.     URL = "https://wwwfile.megabank.com.tw/rates/M001/viewF.asp"
  7.     With CreateObject("InternetExplorer.Application")
  8.         .Visible = True     '  是否顯示 IE
  9.         .Navigate URL
  10.          Do While .ReadyState <> 4 Or .Busy
  11.                 DoEvents
  12.             Loop
  13.         Do While .ReadyState <> 4 Or .Busy: DoEvents: Loop
  14.         xlHtm = .Document.body.innerHTML                '儲存
  15.         Set A = .Document.getElementsBytagname("table")
  16.         For xi = 0 To 2
  17.             .Document.body.innerHTML = A(xi).outerHTML
  18.             .ExecWB 17, 2       '  Select All
  19.             .ExecWB 12, 2       '  Copy selection
  20.             With shts
  21.                 .Range("A" & .[A65535].End(xlUp).Row + 1).Select
  22.                 .PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  23.             End With
  24.             .Document.body.innerHTML = xlHtm                  '還原
  25.         Next xi
  26.         shts.Cells.EntireColumn.AutoFit     '  自動調整欄寬
  27.         .Quit
  28.     End With
  29. End Sub
複製代碼

TOP

        靜思自在 : 為人處世要小心細心,但不要「小心眼」。
返回列表 上一主題