返回列表 上一主題 發帖

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

回復 8# GBKEE
版大這個程式真的很好用,
我是EXCEL VBA初學者,還在模索階段…痛苦學習中
可以懇請版大在大力幫忙一下,如何能一次下載最新8季的資料及在開頭列出股名或代號
感激不盡,謝謝

TOP

回復 21# chang0833
請問版大,我用最笨的方式複製了八次程式(固定年度及季別),總於可以跑出來了,可是"股票代號"可否設計一個可以自用調整的變數來輸入
"xCo_Id = "K"                   '要求輸入網頁的參數:股票代號"……這一行該如何寫,或者有其它更好的方式。

煩請版大賜教了,感激不盡,謝謝。

Dim URL As String, xCo_Id As String, xSyear As String, xSseason As String

xCo_Id = "K"                   '要求輸入網頁的參數:股票代號
xSyear = "102"    'Format(Date, "e")->中華民國的年度
xSseason = "2"       'Format(Date, "q")->當年度的季別
URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
    With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("A1"))
        .AdjustColumnWidth = True                  '自動調整欄寬
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "2,3,4"                 '資產負債表,綜合損益表,現金流量表
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

TOP

"xCo_Id = "K"                   '要求輸入網頁的參數:股票代號"……這一行該如何寫,或者有其它更好的方式
回復 22# chang0833

參考 這裡  改一下
  1. X = Application.InputBox("請輸入篩選關鍵字")
  2. If X = "" Or X = "False" Then Exit Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 22# chang0833
感謝版大,代號輸入問題已經解決^^
再請版大解決一個問題,要如何使下載下來的資料,放在指定的儲存格位置??
煩請賜教了,謝謝^^

TOP

版大:我執行後,網頁資料都是從第一列開始排列,在執行到下一季時會把資料往右推
,但遇到財報第四季時,因其格式與其他三季不同,導致更新資料起始不同而改變資料位置
,所以我需要把每一季的下載位置,都放在固定儲存位置,資料才不會每次都不一樣
在勞煩版大了^^謝謝

Dim URL As String, xCo_Id As String, xSyear As String, xSseason As String

K = Application.InputBox("請輸入股票代號")
X = Application.InputBox("請輸入最新年度")
Y = X - 1
Z = X - 2

xCo_Id = "" & K                 '要求輸入網頁的參數:股票代號
xSyear = "" & Z    'Format(Date, "e")->中華民國的年度
xSseason = "2"       'Format(Date, "q")->當年度的季別
URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
    With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("A1"))
        .AdjustColumnWidth = True                  '自動調整欄寬
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "2,3,4"                 '資產負債表,綜合損益表,現金流量表
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
xCo_Id = "" & K                     '要求輸入網頁的參數:股票代號
xSyear = "" & Z    'Format(Date, "e")->中華民國的年度
xSseason = "3"       'Format(Date, "q")->當年度的季別
URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
    With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("A1"))
        .AdjustColumnWidth = True                  '自動調整欄寬
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "2,3,4"                 '資產負債表,綜合損益表,現金流量表
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
xCo_Id = "" & K                     '要求輸入網頁的參數:股票代號
xSyear = "" & Z 'Format(Date, "e")->中華民國的年度
xSseason = "4"       'Format(Date, "q")->當年度的季別
URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
    With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("A1"))
        .AdjustColumnWidth = True                  '自動調整欄寬
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "2,3,4"                 '資產負債表,綜合損益表,現金流量表
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
xCo_Id = "" & K                     '要求輸入網頁的參數:股票代號
xSyear = "" & Y    'Format(Date, "e")->中華民國的年度
xSseason = "1"       'Format(Date, "q")->當年度的季別
URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
    With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("A1"))
        .AdjustColumnWidth = True                  '自動調整欄寬
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "2,3,4"                 '資產負債表,綜合損益表,現金流量表
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

TOP

回復 25# chang0833
已修改 試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As String, xSyear As String, X As Integer, Rng As Range
  4.     Dim xSseason As Variant
  5.     xCo_Id = Application.InputBox("請輸入股票代號", , 2303)         '預設為 2303
  6.     X = Application.InputBox("請輸入最新年度", , Year(Date) - 1911) '中華民國的年度
  7.    
  8.     With ActiveSheet
  9.         For Each xSseason In .QueryTables 'WEB查詢物件集合
  10.             xSseason.Delete
  11.         Next
  12.         For Each xSseason In .Names       'Name 物件的集合
  13.             .Names(xSseason.Name).Delete
  14.         Next
  15.         .UsedRange.Clear
  16.         Set Rng = .Range("a1") '指定工作表上 WEB查詢的位置
  17.     End With
  18.     '''''''''''''''''''
  19.     For Each xSseason In Array(2, 3, 4, 1) '迴圈xSseason => 2, 3, 4, 1
  20.         xSyear = "" & IIf(xSseason > 1, X - 2, X - 1)
  21.         URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  22.         With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Rng)
  23.             .Name = xCo_Id & "-" & xSyear & "- 第 " & xSseason & " 季" 'WEB查詢的名稱
  24.             .AdjustColumnWidth = True                  '自動調整欄寬
  25.             .WebSelectionType = xlSpecifiedTables
  26.             .WebFormatting = xlWebFormattingNone
  27.             .WebTables = "2,3,4"                  '資產負債表,綜合損益表,現金流量表
  28.             .WebPreFormattedTextToColumns = True
  29.             .WebConsecutiveDelimitersAsOne = True
  30.             .WebSingleBlockTextImport = False
  31.             .WebDisableDateRecognition = False
  32.             .WebDisableRedirections = False
  33.             .Refresh BackgroundQuery:=False
  34.             With .ResultRange      'WEB查詢資料的範圍
  35.                 Set Rng = .Cells(.Rows.Count + 2, 1) '下一WEB查詢的位置
  36.             End With
  37.         End With
  38.     Next
  39. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 26# GBKEE
感謝版大的熱心回復,減輕很多學習上的困擾^^
再請教一下版大所寫的這段程式,只能將輸入最新的年度往前查前二年的資料
,但今年的資料會無法查詢,該如何讓今年的資料也可以下載,
在勞煩版大了,謝謝^^


    For Each xSseason In Array(2, 3, 4, 1) '迴圈xSseason => 2, 3, 4, 1

        xSyear = "" & IIf(xSseason > 1, X - 2, X - 1)

TOP

回復 27# chang0833
試試看(全部)
  1. Option Explicit
  2. Sub Ex()
  3.     Dim URL As String, xCo_Id As String, X As Integer, Rng As Range
  4.     Dim E As Variant, xSyear As Integer, xSseason As Integer, D_Name As String
  5.    
  6.     With ActiveSheet
  7.         For Each E In .QueryTables 'WEB查詢物件集合
  8.             E.Delete
  9.         Next
  10.         For Each E In .Names       'Name 物件的集合
  11.             .Names(E.Name).Delete
  12.         Next
  13.         .UsedRange.Clear
  14.         Set Rng = .Range("a1") '指定工作表上 WEB查詢的位置
  15.     End With
  16.     xCo_Id = Application.InputBox("請輸入股票代號", , 2303)         '預設為 2303
  17.     X = Year(Date) - 1910                  '中華民國的年度
  18.     For xSyear = X To X - 3 Step -1        '迴圈:年度    '105->102
  19.     'For xSyear = X - 3 To X               '迴圈:年度    '102->105
  20.         For xSseason = 1 To 4 '             '迴圈:季別    '1,2,3,4
  21.             URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
  22.             With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Rng)
  23.                 .Name = xCo_Id & "_" & xSyear & "_第" & xSseason & "季" 'WEB查詢的名稱
  24.                 .AdjustColumnWidth = True                  '自動調整欄寬
  25.                 .WebSelectionType = xlSpecifiedTables
  26.                 .WebFormatting = xlWebFormattingNone
  27.                 .WebTables = "2,3,4"                  '資產負債表,綜合損益表,現金流量表
  28.                 .WebPreFormattedTextToColumns = True
  29.                 .WebConsecutiveDelimitersAsOne = True
  30.                 .WebSingleBlockTextImport = False
  31.                 .WebDisableDateRecognition = False
  32.                 .WebDisableRedirections = False
  33.                 .Refresh BackgroundQuery:=False
  34.                 If .ResultRange.Rows.Count = 2 Then '無資料
  35.                     D_Name = .Name                  'WEB查詢的名稱
  36.                     .Delete                         '刪除:WEB查詢
  37.                     With Rng.Parent
  38.                         For Each E In .Names
  39.                             If InStr(E.Name, D_Name) Then E.Delete '刪除:工作表上的名稱->WEB查詢的名稱
  40.                         Next
  41.                     End With
  42.                 Else
  43.                     With .ResultRange      'WEB查詢資料的範圍
  44.                         Set Rng = .Cells(.Rows.Count + 2, 1) '下一WEB查詢的位置
  45.                     End With
  46.                 End If
  47.             End With
  48.         Next
  49.     Next
  50.     MsgBox "Ok"
  51. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 28# GBKEE

感謝版大,目前下載沒問題了,謝謝^^
目前先來慢慢研究版大的程式,太感恩了^^

TOP

回復 29# chang0833
回復 28# GBKEE

GBKEE 版大
假設如果將:
    X = Year(Date) - 1910                 '  中華民國的年度
    For xSyear = X To X - 3 Step -1       ' 迴圈: 年度    ' 105->102
修改為:
    X = Year(Date) - 1911                 '  中華民國的年度
    For xSyear = X To X - 2 Step -1       ' 迴圈: 年度    ' 104->102  最新的年度往前查前二年的資料
則本年度資訊會從 『"$A$1"』位址開始列印,否則會從 『"$A$13"』位址開始列印。
這隻程式非常精簡易覽,我會好好保存備錄的,謝謝 GBKEE 您的指導!
  1. "$A$1"     2015年3月31日        2014年12月31日        2014年3月31日   
  2. "$A$282"   2015年6月30日        2014年12月31日        2014年6月30日
  3. "$A$567"   2015年9月30日        2014年12月31日        2014年9月30日
  4. "$A$856"   2015年第3季        2014年第3季        2015年01月01日至2015年09月30日        2014年01月01日至2014年09月30日

  5. "$A$859"   2014年3月31日        2013年12月31日        2013年3月31日
  6. "$A$1133"  2014年6月30日        2013年12月31日        2013年6月30日
  7. "$A$1409"  2014年9月30日        2013年12月31日        2013年9月30日
  8. "$A$1688"  2014年12月31日        2013年12月31日       

  9. "$A$1971"  2013年3月31日        2012年12月31日        2012年3月31日        2012年1月1日
  10. "$A$2247"  2013年6月30日        2012年12月31日        2012年6月30日        2012年1月1日
  11. "$A$2530"  2013年9月30日        2012年12月31日        2012年9月30日        2012年1月1日
  12. "$A$2812"  2013年12月31日        2012年12月31日        2012年1月1日
  13.     ~
  14. "$A$3092"  
複製代碼

TOP

        靜思自在 : 一個缺口的杯子,如果換一個角度看它,它仍然是圓的。
返回列表 上一主題