返回列表 上一主題 發帖

新版股市公開資訊觀測站的資料抓到EXECL?

回復 38# chang0833
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As String, X As Integer
  4.     Dim xSyear As Integer, xSseason As Integer, Ar(1 To 4)
  5.     Dim Sh(1 To 2) As Worksheet, AY, Rng As Range, E As Variant
  6.     Dim Wb As Workbook
  7.     For X = 0 To 3
  8.         Ar(X + 1) = 1 + (6 * X)  '第一季到第四季的欄位
  9.     Next
  10.     xCo_Id = Application.InputBox("請輸入股票代號", , 2303)         '預設為 2303
  11.     X = Year(Date) - 1910                 '中華民國的年度
  12.     Application.ScreenUpdating = False
  13.     Set Wb = Workbooks("book1.xls")           '指定活頁簿
  14.     With Wb
  15.         Set Sh(1) = .Sheets.Add               '新增工作表: 複製季財報到指定工作頁
  16.         Set Sh(2) = .Sheets.Add               '新增工作表:  WEB查詢用
  17.     End With
  18.     On Error GoTo Er                        '處理程式上的錯誤
  19.     Sh(1).Name = xCo_Id & "季報表"          '這名稱工作表如已存在程式會有錯誤
  20.     On Error GoTo 0                         '不在處理程式上的錯誤
  21.    
  22.     For xSyear = X To X - 3 Step -1        '迴圈:年度    '105->102
  23.     'For xSyear = X - 3 To X               '迴圈:年度    '102->105
  24.         For xSseason = 1 To 4 '             '迴圈:季別    '1,2,3,4
  25.             URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  26.             With Sh(2).QueryTables.Add(Connection:=URL, Destination:=Sh(2).[A1])
  27.                 .Name = xCo_Id & "_" & xSyear & "_第" & xSseason & "季" 'WEB查詢的名稱
  28.                 .AdjustColumnWidth = True                  '自動調整欄寬
  29.                 .WebSelectionType = xlSpecifiedTables
  30.                 .WebFormatting = xlWebFormattingNone
  31.                 .WebTables = "2,3,4"                  '資產負債表,綜合損益表,現金流量表
  32.                 .WebPreFormattedTextToColumns = True
  33.                 .WebConsecutiveDelimitersAsOne = True
  34.                 .WebSingleBlockTextImport = False
  35.                 .WebDisableDateRecognition = False
  36.                 .WebDisableRedirections = False
  37.                 .Refresh BackgroundQuery:=False
  38.                 If .ResultRange.Rows.Count > 2 Then '有資料
  39.                     Set Rng = Sh(1).Cells(1, Ar(xSseason)).Cells(Rows.Count).End(xlUp)
  40.                     If Rng.Row > 1 Then Set Rng = Rng.Offset(2)
  41.                     .ResultRange.Copy Rng
  42.                 Else
  43.                     .Delete
  44.                 End If
  45.                
  46.             End With
  47.         Next
  48.     Next
  49.     Application.DisplayAlerts = False
  50.     Sh(2).Delete
  51.     Application.DisplayAlerts = True
  52.     Application.ScreenUpdating = True
  53.     Sh(1).Parent.Save
  54.     MsgBox "Ok"
  55.     Exit Sub
  56. Er:     '處理 xCo_Id &季報表 工作表已存在
  57.     Application.DisplayAlerts = False
  58.     Sheets(xCo_Id & "季報表").Delete
  59.     Application.DisplayAlerts = True
  60.    Resume Next  '回到錯誤的程式碼
  61. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 40# chang0833
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Wb As Workbook
  4.     Set Wb = ActiveWorkbook                         '作用中活頁簿
  5.     Set Wb = ThisWorkbook                           '程式碼所在的活頁簿
  6.     Set Wb = Workbooks("book1")                     '已開啟了的活頁簿中,指定的活頁簿
  7.     Set Wb = Workbooks.Open("D:\試算表\Book1.xls")  '要開啟的活頁簿
  8.    
  9.     Set Wb = Workbooks("Book1.xls")                 '已開啟了的活頁簿中,指定的活頁簿
  10.    
  11. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 42# chang0833
是這樣嗎?
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As String
  4.     Dim xSyear As Integer, xSseason As Integer
  5.     Dim Sh(1 To 2) As Worksheet, Rng As Range
  6.    
  7.     xCo_Id = Application.InputBox("請輸入股票代號", , 2303)         '預設為 2303
  8.     xSyear = Format(Date, "E")                 '中華民國的年度
  9.     xSseason = DatePart("q", Date)             '當季
  10.     Application.ScreenUpdating = False
  11.     'Set Wb = ThisWorkbook           '指定活頁簿
  12.     With ThisWorkbook           '指定活頁簿
  13.         Set Sh(1) = .Sheets.Add               '新增工作表: 複製季財報到指定工作頁
  14.         Set Sh(2) = .Sheets.Add               '新增工作表:  WEB查詢用
  15.     End With
  16.     On Error GoTo Er                        '處理程式上的錯誤
  17.     Application.DisplayAlerts = False
  18.     Sh(1).Name = xCo_Id & "季報表"          '這名稱工作表如已存在程式會有錯誤
  19.     Set Rng = Sh(1).[A1]
  20.     On Error GoTo 0                         '不再處理程式上的錯誤
  21.    
  22.     Do
  23.             URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  24.             With Sh(2).QueryTables.Add(Connection:=URL, Destination:=Sh(2).[A1])
  25.                 .Name = xCo_Id & "_" & xSyear & "_第" & xSseason & "季" 'WEB查詢的名稱
  26.                 .AdjustColumnWidth = True                  '自動調整欄寬
  27.                 .WebSelectionType = xlSpecifiedTables
  28.                 .WebFormatting = xlWebFormattingNone
  29.                 .WebTables = "2,3,4"                  '資產負債表,綜合損益表,現金流量表
  30.                 .WebPreFormattedTextToColumns = True
  31.                 .WebConsecutiveDelimitersAsOne = True
  32.                 .WebSingleBlockTextImport = False
  33.                 .WebDisableDateRecognition = False
  34.                 .WebDisableRedirections = False
  35.                 .Refresh BackgroundQuery:=False
  36.                 If .ResultRange.Rows.Count > 2 Then '有資料
  37.                     Debug.Print xSyear, xSseason, Rng.Address
  38.                     .ResultRange.Copy Rng
  39.                      Set Rng = Rng.Offset(, .ResultRange.Columns.Count + 1)
  40.                 Else
  41.                     .Delete
  42.                 End If
  43.             End With
  44.             xSseason = xSseason - 1
  45.             If xSseason = 0 Then
  46.                 xSseason = 4
  47.                 xSyear = xSyear - 1
  48.             End If
  49.     Loop Until xSyear = Format(Date, "E") - 3
  50.    
  51.     Sh(2).Delete
  52.     Application.DisplayAlerts = True
  53.     Application.ScreenUpdating = True
  54. '   Sh(1).Parent.Save
  55.     MsgBox "Ok"
  56.     Exit Sub
  57. Er:     '處理 xCo_Id &季報表 工作表已存在
  58.     Sheets(xCo_Id & "季報表").Delete '覆蓋原工作頁嗎?(只留下最後更新的資料)
  59.    
  60.     Resume     '回到錯誤的程式碼
  61. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2016-12-24 09:07 編輯

回復 47# jasonwu0114
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As Range, xSyear As String, xSseason As String, i As Integer, M As Variant
  4.     xSyear = 105
  5.     xSseason = 3
  6.     For i = 1 To 20  '跑20-30個股票代號
  7.         Set xCo_Id = Sheets(1).Cells(i, "A")  '股票代號
  8.         URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  9.         With SheetS(2).QueryTables.Add(Connection:=URL, Destination:=Sheets(2).Range("A1"))
  10.             .AdjustColumnWidth = False                   '自動調整欄寬
  11.             .WebSelectionType = xlSpecifiedTables
  12.             .WebFormatting = xlWebFormattingNone
  13.             .WebTables = "2"   ',3,4"                 '資產負債表,綜合損益表,現金流量表
  14.             .WebPreFormattedTextToColumns = True
  15.             .WebConsecutiveDelimitersAsOne = True
  16.             .WebSingleBlockTextImport = False
  17.             .WebDisableDateRecognition = False
  18.             .WebDisableRedirections = False
  19.             .Refresh BackgroundQuery:=False
  20.             With .ResultRange  '所匯入資料的範圍
  21.                 M = Application.Match("*應付短期票券合計", .Columns(1), 0)  '工作表函數在第一欄中傳回比對到的欄位
  22.                 If IsNumeric(M) Then xCo_Id.Offset(, 1) = .Cells(M, "b")             '有比對到傳回的欄位的數字
  23.                 .Clear
  24.             End With
  25.             .Parent.Names(.Name).Delete          '刪除工作表的名稱
  26.             .Delete                                             ''這QueryTable刪除掉
  27.         End With
  28.     Next
  29. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題