返回列表 上一主題 發帖

[發問] 有辦法將每日資料會成一份嘛

回復 10# pupai


    謝謝GBKEE版主的分享
兩個問題請教
1.D欄文字轉數字
2.另外下載完後的資料,自動儲存到另一個SHEET(以股票命名)
謝謝您

TOP

回復 11# pupai
這都可以用巨集錄製試試看,練習才會進步的.
************************************
1.新增一工作表,將之命名為股票代號.         
2將下載的資料,複製到,此新工作表
*************************************
1.原程式中有新增工作表,可將之命名為股票代號.
2,將下載的資料,複製到,此新增工作表
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 12# GBKEE


    GBKEE版主
我有用巨集完成(如附件)
1.D欄文字轉數字
2.下載完後的資料,自動儲存到另一個SHEET(以股票命名)
但我寫知道VBA要如何完成上面的動作
另外我有個新問題
如果我在一個代碼分頁中(A1欄輸入股票代碼1101,1102,1103,1104,1108,1109,1110,1201,1203…等等)
VBA可以產生一個迴圈一次幫我跑完這一些股票代碼的歷年資料,並再自動儲存在另一個SHEET(以股票命名)?
謝謝!!

個股日本益比、殖利率及股價淨值比.rar (18.04 KB)

TOP

回復 13# pupai
你說:我有用巨集完成(如附件),有嗎?哪有完成.



  1. Option Explicit
  2. Sub GetData()
  3.     Dim DataSheet As Worksheet, Sh As Worksheet, Msg As Boolean
  4.     Dim EndDate As Date, StartDate As Date, AR, xR As Long
  5.     Dim Symbol As Variant, Qur As String
  6.     Set DataSheet = Sheets("代碼")
  7.     With DataSheet
  8.         StartDate = .[C1]
  9.         EndDate = .[C2]
  10.         '本資料自民國94年09月01日開始提供 *** 除錯  ***
  11.         If StartDate < #9/1/2005# Or EndDate < #9/1/2005# Or StartDate > EndDate Or EndDate > Date Then
  12.             MsgBox "數據有誤" & IIf(StartDate < #9/1/2005#, vbLf & "StartDate :日期 小於 94年09月01日  ", "") & _
  13.             IIf(EndDate < #9/1/2005#, vbLf & "EndDate :日期 小於 94年09月01日  ", "") & _
  14.             IIf(StartDate > EndDate, vbLf & "StartDate > EndDate", "") & _
  15.             IIf(EndDate > Date, vbLf & " EndDate >" & Date, "")
  16.             Exit Sub
  17.         End If
  18.     '*********************************************
  19.         Application.DisplayAlerts = False
  20.         For Each Sh In Sheets
  21.             If Sh.Name <> DataSheet.Name Then Sh.Delete    '刪除不必要的工作
  22.         Next
  23.         For Each Symbol In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))  '股票的迴圈
  24.             Msg = False
  25.             StartDate = .[C1]                           '迴圈需重新回到原本的 StartDate日期
  26.             Set Sh = Sheets.Add(, Sheets(Sheets.Count)) '新增的工作表位於活頁簿最後面(Sheets.Count)
  27.             DataSheet.Activate
  28.             
  29.             Do While DateSerial(Year(StartDate), Month(StartDate), 1) <= EndDate
  30.                 Qur = "http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU.php?myear=" & Format(StartDate, "yyyy") & "&mmon=" & Format(StartDate, "m") & "&STK_NO=" & Symbol
  31.                 With Sh       '新增的工作表
  32.                     If .QueryTables.Count = 0 Then                      'Web查詢
  33.                         .QueryTables.Add "URL;" & Qur, .[M1]            'Web查詢資料在M欄
  34.                     Else
  35.                         .QueryTables(1).Connection = "URL;" & Qur
  36.                         Msg = True
  37.                     End If
  38.                     With .QueryTables(1)
  39.                         .WebFormatting = xlWebFormattingNone
  40.                         .WebSelectionType = xlSpecifiedTables
  41.                         .WebDisableDateRecognition = True
  42.                         .WebTables = "8"
  43.                         .Refresh BackgroundQuery:=False
  44.                         If Application.CountA(.ResultRange) = 0 Then Msg = False
  45.                         If Msg Then
  46.                             AR = .ResultRange.Offset(2)
  47.                         Else
  48.                             AR = .ResultRange
  49.                         End If
  50.                         xR = Application.CountA(.Parent.[d:d]) + 1                         '.Parent :Web查詢的父層
  51.                         .Parent.Cells(xR, "A").Resize(UBound(AR, 1), UBound(AR, 2)) = AR   '資料複製到 新增工作表的A欄
  52.                     End With
  53.                 End With
  54.                 StartDate = DateAdd("m", 1, StartDate)   '日期 + 1個月
  55.             Loop
  56.             With Sh
  57.                 .Name = Symbol                                 '以股票命名
  58.                 .QueryTables(1).ResultRange = ""               '清除Web查詢的資料
  59.                  .Names(.QueryTables(1).Name).Delete           'Web查詢的名稱
  60.             End With
  61.         Next
  62.     End With
  63.     Application.DisplayAlerts = True
  64. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 14# GBKEE


    G大您好
再次感謝您的熱心的指導
我研究看看
謝謝...

TOP

  1. 程式我大概說明一下

  2. Option Explicit
  3. Sub GetData()
  4.     Dim DataSheet As Worksheet, Sh As Worksheet, Msg As Boolean               '定義變數
  5.     Dim EndDate As Date, StartDate As Date, i As Integer, AR, xR As Long    '定義變數
  6.     Dim Symbol As String, Qur As String                                                            '定義變數
  7.     Set DataSheet = Sheets("Sheet1")                                                                   '設定工作表名稱
  8.     With DataSheet
  9.         StartDate = .[b1]                                                                                          '將B1欄位值讀到變數StartDate中
  10.         EndDate = .[b2]                                                                                           '將B2欄位值讀到變數EndDate中
  11.         Symbol = .[b3]                                                                                             '將B3欄位值讀到變數Symbol中
  12.         .Range("D1").CurrentRegion = ""                                                               '設定D1欄位值
  13.     End With
  14.     '本資料自民國94年09月01日開始提供 *** 除錯  ***                                '以下以94/09/01為界限,判讀輸入值是否有錯
  15.     If StartDate < #9/1/2005# Or EndDate < #9/1/2005# Or Len(Symbol) <= 3 Or StartDate > EndDate Or EndDate > Date Then
  16.         MsgBox "數據有誤" & IIf(StartDate < #9/1/2005#, vbLf & "StartDate :日期 小於 94年09月01日  ", "") & _
  17.         IIf(EndDate < #9/1/2005#, vbLf & "EndDate :日期 小於 94年09月01日  ", "") & _
  18.         IIf(Len(Symbol) <= 3, vbLf & "Symbol : 股票代號 ", "") & _
  19.         IIf(StartDate > EndDate, vbLf & "StartDate > EndDate", "") & _
  20.         IIf(EndDate > Date, vbLf & " EndDate >" & Date, "")
  21.         Exit Sub
  22.     End If
  23.     '*********************************************
  24.     Set Sh = Sheets.Add(Sheets(1))
  25.     DataSheet.Activate
  26.     Do While DateSerial(Year(StartDate), Month(StartDate), 1) <= EndDate   ' DO到LOOP中的程式為以輸入值去抓取網頁的資料
  27.         Qur = "http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU.php?myear=" & Format(StartDate, "yyyy") & "&mmon=" & Format(StartDate, "m") & "&STK_NO=" & Symbol
  28.         With Sh
  29.             If .QueryTables.Count = 0 Then
  30.                 .QueryTables.Add "URL;" & Qur, .[A1]
  31.             Else
  32.                 .QueryTables(1).Connection = "URL;" & Qur
  33.                 Msg = True
  34.             End If
  35.            With .QueryTables(1)
  36.                 .WebFormatting = xlWebFormattingNone
  37.                 .WebSelectionType = xlSpecifiedTables
  38.                 .WebDisableDateRecognition = True
  39.                 .WebTables = "8"
  40.                  .Refresh BackgroundQuery:=False
  41.                  If Application.CountA(.ResultRange) = 0 Then Msg = False
  42.                  If Msg Then
  43.                     AR = .ResultRange.Offset(2)
  44.                  Else
  45.                     AR = .ResultRange
  46.                  End If
  47.                  With DataSheet
  48.                     xR = Application.CountA(.[d:d]) + 1
  49.                     .Cells(xR, "D").Resize(UBound(AR, 1), UBound(AR, 2)) = AR
  50.                  End With
  51.            End With
  52.            End With
  53.         StartDate = DateAdd("m", 1, StartDate)                            '這行就是能抓跨月的計算式,抓完第一個月後,變數StartDate的月份+1
  54.     Loop
  55.     Application.DisplayAlerts = False
  56.     Sh.Delete
  57.     Application.DisplayAlerts = True
  58. End Sub
複製代碼
分享G大的程式碼

TOP

回復 16# pupai
你還是沒搞清楚CurrentRegion是什麼,須多看vba的說明.
  1.   .Range("D1").CurrentRegion = ""                                                               '設定D1欄位值
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 17# GBKEE


    呵呵....苦笑 我再努力.   受教了

TOP

如何避免當查詢日期 小於 資料日期時 程式中斷

您好
代碼1773的歷史資料從2009/2/27開始有資料
假設我在StartDate打入2006/1/1
當程式執行到1773就會中斷
請問要如何修改
謝謝!!

test.rar (18.45 KB)

TOP

本帖最後由 GBKEE 於 2013-10-23 16:18 編輯

回復 19# pupai
  1. Option Explicit
  2. Sub GetData()
  3.     Dim DataSheet As Worksheet, Sh As Worksheet
  4.     Dim EndDate As Date, StartDate As Date, AR, xR As Long
  5.     Dim Symbol As Variant, Qur As String
  6.     Set DataSheet = Sheets("代碼")
  7.     With DataSheet
  8.         StartDate = .[C1]
  9.         EndDate = .[C2]
  10.         '本資料自民國94年09月01日開始提供 *** 除錯  ***
  11.         If StartDate < #9/1/2005# Or EndDate < #9/1/2005# Or StartDate > EndDate Or EndDate > Date Then
  12.             MsgBox "數據有誤" & IIf(StartDate < #9/1/2005#, vbLf & "StartDate :日期 小於 94年09月01日  ", "") & _
  13.             IIf(EndDate < #9/1/2005#, vbLf & "EndDate :日期 小於 94年09月01日  ", "") & _
  14.             IIf(StartDate > EndDate, vbLf & "StartDate > EndDate", "") & _
  15.             IIf(EndDate > Date, vbLf & " EndDate >" & Date, "")
  16.             Exit Sub
  17.         End If
  18.     '*********************************************
  19.         Application.DisplayAlerts = False
  20.         For Each Sh In Sheets
  21.             If Sh.Name <> DataSheet.Name Then Sh.Delete    '刪除不必要的工作
  22.         Next
  23.         For Each Symbol In .Range("A2", .Range("A" & .Rows.Count).End(xlUp))  '股票的迴圈
  24.             StartDate = .[C1]                           '迴圈需重新回到原本的 StartDate日期
  25.             Set Sh = Sheets.Add(, Sheets(Sheets.Count)) '新增的工作表位於活頁簿最後面(Sheets.Count)
  26.             DataSheet.Activate
  27.             Do While DateSerial(Year(StartDate), Month(StartDate), 1) <= EndDate
  28.                 Qur = "http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU.php?myear=" & Format(StartDate, "yyyy") & "&mmon=" & Format(StartDate, "m") & "&STK_NO=" & Symbol
  29.                 With Sh       '新增的工作表
  30.                     If .QueryTables.Count = 0 Then                      'Web查詢
  31.                         .QueryTables.Add "URL;" & Qur, .[M1]            'Web查詢資料在M欄
  32.                     Else
  33.                         .QueryTables(1).Connection = "URL;" & Qur
  34.                     End If
  35.                     With .QueryTables(1)
  36.                         .WebFormatting = xlWebFormattingNone
  37.                         .WebSelectionType = xlSpecifiedTables
  38.                         .WebDisableDateRecognition = True
  39.                         .WebTables = "7,8"
  40.                         .Refresh BackgroundQuery:=False
  41.                          If Application.CountA(.ResultRange) > 1 Then
  42.                             AR = .ResultRange.Offset(4)
  43.                             If Application.CountA(.Parent.[a:a]) = 0 Then AR = .ResultRange.Offset(3)
  44.                             xR = Application.CountA(.Parent.[a:a]) + 1                         '.Parent :Web查詢的父層
  45.                             .Parent.Cells(xR, "A").Resize(UBound(AR, 1), UBound(AR, 2)) = AR   '資料複製到 新增工作表的A欄
  46.                         End If
  47.                     End With
  48.                     
  49.                 End With

  50.                 StartDate = DateAdd("m", 1, StartDate)   '日期 + 1個月
  51.             Loop
  52.             With Sh
  53.                 .Name = Symbol                                 '以股票命名
  54.                 '------------------------
  55.                 .Activate
  56.                 .Range("E3").Select
  57.                 ActiveCell.FormulaR1C1 = _
  58.                 "=IF(ISERROR(DATEVALUE(1911+MID(RC[-4],1,FIND(""/"",RC[-4])-1) & MID(RC[-4],FIND(""/"",RC[-4]),LEN(RC[-4])))),"""",DATEVALUE(1911+MID(RC[-4],1,FIND(""/"",RC[-4])-1) & MID(RC[-4],FIND(""/"",RC[-4]),LEN(RC[-4]))))"
  59.                 .Range("E3").Select
  60.                 Selection.Copy
  61.                 .Columns("E:E").Select
  62.                 ActiveSheet.Paste
  63.                 .Columns("E:E").Select
  64.                 Selection.NumberFormatLocal = "[$-404]e/m/d;@"
  65.                 '------------------------
  66.                 .QueryTables(1).ResultRange = ""               '清除Web查詢的資料
  67.                  .Names(.QueryTables(1).Name).Delete           'Web查詢的名稱
  68.             End With
  69.         Next
  70.     End With
  71.     Application.DisplayAlerts = True
  72. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 生氣,就是拿別人的過錯來懲罰自己。
返回列表 上一主題