返回列表 上一主題 發帖

股價代號和起始日期的抓取

超級版主 您好
我在run...每日收盤資料那個巨集...出現無法執行
用除錯找出...  .Refresh BackgroundQuery:=False
那是否要更改為 true

TOP

回復 11# GBKEE


    感謝G大,收下研究,^^
用簡單的心做複雜的事

TOP

回復 2# yuhuahsiao

Hi Y大您好,
          我使用錄製巨集,但網址顯示也是 ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://www.cnyes.com/twstock/directorholder/1101.htm", Destination:= _
        Range("$A$1"))
以網址來看,只能更改股票代號,月份方面就無法更改...
請問我該如何使用VBA,在儲存格輸入月份,就會帶出資料...請幫忙解惑.

謝謝^^
BigDog
用簡單的心做複雜的事

TOP

本帖最後由 GBKEE 於 2016-4-26 10:25 編輯

回復 9# BigDog
2016/4/26 更新
  1. Option Explicit
  2. Sub 鉅亨網_董監持股()
  3.     Dim ie As Object, A As Object, T As Date, i As Integer, c As Integer
  4.     Dim Code As String, xMonth As String
  5.     Code = InputBox("輸入股票代號 :  ", "股票代號", 2303)
  6.     xMonth = InputBox("輸入年月 :  ", "年月", Format(DateAdd("M", -2, Date), "yyyy年m月"))
  7.     Set ie = CreateObject("InternetExplorer.Application")
  8.     ie.Navigate "http://www.cnyes.com/twstock/directorholder/" & Code & ".htm"
  9.    '  ie.Visible = True
  10.     Do While ie.Busy Or ie.ReadyState <> 4: DoEvents: Loop
  11.     With ie.Document
  12.          With .getElementById("ctl00_ContentPlaceHolder1_DropDownList1")
  13.                 .Value = xMonth
  14.                 .fireEvent ("onchange")
  15.         End With
  16.         Do While ie.Busy Or ie.ReadyState <> 4
  17.              DoEvents
  18.         Loop
  19.         On Error Resume Next
  20.         Do
  21.                 DoEvents
  22.                 Set A = .getelementsbytagname("table")(1)
  23.                 If Not A Is Nothing Then
  24.                         If InStr(A.innertext, Format(xMonth, "YYYYM")) Then
  25.                         If Err Then
  26.                            Debug.Print Err
  27.                             Err.Clear
  28.                         Else
  29.                             Exit Do
  30.                         End If
  31.                         End If
  32.                 End If
  33.         Loop
  34.         On Error GoTo 0
  35.         Cells.Clear
  36.         Cells(1, "A").Resize(, 4) = Array("股票代號", Code, "月份", xMonth)
  37.         For i = 0 To A.Rows.Length - 1
  38.             For c = 0 To A.Rows(i).Cells.Length - 1
  39.                 Cells(i + 2, c + 1) = A.Rows(i).Cells(c).innertext
  40.             Next
  41.         Next
  42.     End With
  43.       ie.Quit
  44.     MsgBox "OK"
  45. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 1# BigDog


    這個應該無法用iqy 抓取
要用VBA

TOP

如何使用.iqy抓此網址資料

請問大家,

        小弟在此網址 http://www.cnyes.com/twstock/directorholder/1101.htm  ,想抓董監持股餘額資料,
但每個月的資料是用下拉式選單選取,網址都不會變動,請問應該如何才能利用.iqy更改參數抓資料...


謝謝各位
BigDog
用簡單的心做複雜的事

TOP

回復 7# 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 Wb As Workbook, Ws As Worksheet, Stock  As Range
  5.     On Error GoTo EX                        '程式執行中有錯誤時跳到 EX: 繼續執行程式
  6.     xDate = Date                            '當日
  7.     Set Sh = Workbooks.Add.Sheets(1)        '新活頁簿的第一個工作表
  8. EX:
  9.     If Err.Number <> 0 Or Msg = True Then   '當日(尚未有資料:錯誤) 或 休市
  10.         xDate = xDate - 1                   '往後退一天一直到開市. ( 假如休市超過1天以上(2,3,4,5,6,春節9天)
  11.         Err.Clear
  12.         Msg = False
  13.     End If
  14.     Startday = Format(xDate, "YYYYMMDD")
  15.     Startmonth = Format(xDate, "YYYYMM")
  16.         '下載網站的網址
  17.     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"))
  18.         .RefreshStyle = xlOverwriteCells                                '抓取網頁的第10個表格作為匯入資料
  19.         .WebTables = "10"
  20.         .Refresh BackgroundQuery:=False                                 '當日(尚未有資料:錯誤)
  21.         If Application.CountA(Sh.QueryTables(1).ResultRange) = 0 Then   '休市 沒有資料
  22.             Msg = True
  23.             GoTo EX
  24.         End If
  25.     End With
  26.     For Each Wb In Workbooks
  27.         For Each Ws In Wb.Sheets
  28.            Set Stock = Sh.[A:A].Find(Ws.Name, lookat:=xlWhole)
  29.             If Not Stock Is Nothing Then
  30.                 Stock.Offset(, 2).Resize(, 14).Copy
  31.                 With Ws
  32.                     With .Range("a" & .Rows.Count).End(xlUp).Offset(1)
  33.                         .Cells = xDate
  34.                         .Offset(, 1).PasteSpecial xlPasteValuesAndNumberFormats
  35.                     End With
  36.                 End With
  37.             End If
  38.         Next
  39.     Next
  40.     Sh.Parent.Close False           '關閉: 新活頁簿的第一個工作表
  41. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

如何將下載的股市每日盤後行情(製作成各股的歷史股價資料)

煩請各位高手專家解惑,謝謝!
請問有辦法將下列程碼的加以修改,把每日各股的盤後行情所得的資料轉換成(各股的歷史資料)。
  1. Sub 下載網站資料()
  2.     Dim Startmonth As String, Startday As String, xDate As Date, Msg As Boolean, Sh As Worksheet
  3.     On Error GoTo EX                        '程式執行中有錯誤時跳到 EX: 繼續執行程式
  4.     xDate = Date                            '當日
  5.     Set Sh = ActiveSheet                    '指定工作表:此程式在任何模組中皆可用
  6. EX:
  7.     If Err.Number <> 0 Or Msg = True Then   '當日(尚未有資料:錯誤) 或 休市
  8.         [color=Red]xDate = xDate - 1    (應改為xDate = Date - 1)[/color]     假如休市超過1天以上(2,3,4,5,6,春節9天),又如何修改呢?         
  9.         Err.Clear
  10.         Msg = False
  11.     End If
  12.     Sh.UsedRange.Clear                       '清理工作表已使用的範圍
  13.     Startday = Format(xDate, "YYYYMMDD")
  14.     Startmonth = Format(xDate, "YYYYMM")
  15.         '下載網站的網址
  16.     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"))
  17.         .RefreshStyle = xlOverwriteCells                            '抓取網頁的第10個表格作為匯入資料
  18.         .WebTables = "10"
  19.         .Refresh BackgroundQuery:=False                             '當日(尚未有資料:錯誤)
  20.         If Application.CountA(Sh.QueryTables(1).ResultRange) = 0 Then  '休市 沒有資料
  21.             Msg = True
  22.             GoTo EX
  23.         End If
  24.     End With
  25.     With Sh.UsedRange
  26.         .Font.Size = 12
  27.         .EntireColumn.AutoFit
  28.     End With
  29. End Sub
複製代碼
因為我已有下載各股歷史股價的資料程式和資料,且依代碼分類成多個活頁簿例如:
workbook(1.xls)有worksheet(1102)、worksheet(1103).....等。1活頁簿中有代碼,第一個數字有[1]的多個工作表。
workbook(2.xls)有worksheet(2102)、worksheet(2103).....等。2活頁簿中有代碼,第一個數字有[2]的多個工作表。
......
......
workbook(9.xls)有worksheet(9102)、worksheet(9103).....等。9活頁簿中有代碼,第一個數字有[9]的多個工作表。
所以想請問有沒有更簡便快速的方法。謝謝各位專家前輩的抽空指導。

TOP

回復 5# edwardlay
2# 看要幾年可修改 -3  看看
  1. 12.        .[b1] = DateAdd("yyyy", -3, d_End)  '下載3年歷史股價的資料
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

請問大大下載時間可以擴大到多長?:)

TOP

        靜思自在 : 人生最大的成就是從失敗中站起來。
返回列表 上一主題