返回列表 上一主題 發帖

[發問] 如何快速將網頁資料抓到excel裡

回復 1# paul3063

試試看
  1.     Sub TWSEtest()
  2.       Dim sh As Worksheet
  3.       Dim iDate$, stock$, yYear$, mMonth$, URL$
  4.       Dim arDATA(), Table
  5.       Dim i%, j%

  6.       Set sh = Sheets("工作表1")
  7.       sh.Activate
  8.       Cells.Clear
  9.       
  10.       stock = "2330"          '設定代號
  11.       yYear = "2017"          '設定年度
  12.       mMonth = "01"           '設定月份
  13.       iDate = yYear & mMonth & "01"
  14.       
  15.       URL = "http://www.twse.com.tw/exchangeReport/STOCK_DAY_AVG?response=Html&date=" & iDate & "&stockNo=" & stock
  16.         
  17.       With CreateObject("InternetExplorer.Application")
  18.             .Visible = False     '  不顯示 IE
  19.             .Navigate URL
  20.             Do While .readyState <> 4
  21.             DoEvents
  22.             Loop
  23.             
  24.             Set Table = .document.getElementsBytagname("table")(0)
  25.             
  26.             With Table
  27.                   ReDim arDATA(.Rows.Length, .Rows(1).Cells.Length)
  28.                   For i = 0 To .Rows.Length - 1
  29.                         For j = 0 To .Rows(1).Cells.Length - 1
  30.                               On Error Resume Next
  31.                               arDATA(i + 1, j + 1) = .Rows(i).Cells(j).innertext
  32.                         Next
  33.                   Next
  34.             End With
  35.             
  36.             sh.Cells(1, 1).Resize(UBound(arDATA), UBound(arDATA, 2)) = arDATA
  37.             [A1].Select
  38.             
  39.             .Quit       '關閉瀏覽器
  40.             
  41.       End With
  42.       
  43.       Set Table = Nothing
  44.       Set sh = Nothing
  45.       Erase arDATA()

  46. End Sub
複製代碼

TOP

        靜思自在 : 【做人的開始】每一天都是故人的開始,每一個時刻都是自己的警惕。
返回列表 上一主題