返回列表 上一主題 發帖

更新股市每日盤後行情最新資料

本帖最後由 GBKEE 於 2013-9-3 07:21 編輯

回復 3# gelai01000001
  1. Option Explicit
  2. Sub 下載網站資料()
  3.     Dim Startmonth As String, Startday As String, xDate As Date, Msg As Boolean, Sh As Worksheet
  4.     Dim T As Date
  5.     T = Time
  6.     On Error Resume Next                    '程式執行中有錯誤時,不裡會繼續執行程式
  7.     xDate = Date                            '當日
  8.     '***************測試用
  9.     xDate = #9/8/2013#                 '休市日  
  10.     '***************測試無誤後可刪除掉
  11.     Set Sh = ActiveSheet                    '指定工作表:此程式在任何模組中皆可用
  12. EX:
  13.     If Err.Number <> 0 Or Msg = True Then   '當日(尚未有資料:錯誤) 或 休市
  14.         xDate = xDate - 1                   '往後退一日
  15.         Err.Clear
  16.         Msg = False
  17.     End If
  18.     Sh.UsedRange.Clear                       '清理工作表已使用的範圍
  19.     Startday = Format(xDate, "YYYYMMDD")
  20.     Startmonth = Format(xDate, "YYYYMM")
  21.         '下載網站的網址
  22.     With Sh.QueryTables.Add(Connection:="URL;http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/genpage/Report" & Startmonth & "/A112" & Startday & "ALLBUT0999_1.php?select2=ALLBUT0999&chk_date=" & Format(xDate, "E/MM/DD"), Destination:=Range("A3"))
  23.         .RefreshStyle = xlOverwriteCells                            '抓取網頁的第10個表格作為匯入資料
  24.         .WebTables = "10"
  25.         .Refresh BackgroundQuery:=False                             '當日(尚未有資料:錯誤)
  26.         If Err.Number <> 0 Then GoTo EX
  27.         If Application.CountA(Sh.QueryTables(1).ResultRange) = 0 Then  '休市 沒有資料
  28.             Msg = True
  29.             GoTo EX
  30.         End If
  31.     End With
  32.     With Sh.UsedRange
  33.         .Font.Size = 12
  34.         .EntireColumn.AutoFit
  35.     End With
  36.     MsgBox "交易的最新日期 " & xDate & vbLf & Application.Text(Time - T, "費時 [SS] 秒")
  37. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 6# gelai01000001
程式會無止盡的跑回圈?不會吧!是資料量大下載費時吧 !!
5#的程式碼已修正試試看
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 我們最大的敵人不是別人.可能是自己。
返回列表 上一主題