返回列表 上一主題 發帖

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

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

您好
我希望可以把每日的資料彙整成一份
請大家幫嘛指導
謝謝!!
  1. Sub GetData()

  2.     Dim DataSheet As Worksheet
  3.     Dim EndDate As Date
  4.     Dim StartDate As Date
  5.     Dim Symbol As String
  6.     Dim qurl As String
  7.     Dim nQuery As Name
  8.    
  9.     Application.ScreenUpdating = False
  10.     Application.DisplayAlerts = False
  11.     Application.Calculation = xlCalculationManual
  12.    
  13.     Set DataSheet = ActiveSheet
  14.   
  15.         StartDate = DataSheet.Range("B1").Value
  16.         EndDate = DataSheet.Range("B2").Value
  17.         Symbol = DataSheet.Range("B3").Value
  18.         Range("C7").CurrentRegion.ClearContents
  19.         
  20.         qurl = "http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU.php?" & Symbol
  21.         qurl = qurl & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & _
  22.             "&c=" & Year(StartDate) & "&d=" & Month(EndDate) - 1 & "&e=" & _
  23.             Day(EndDate) & "&f=" & Year(EndDate) & "&g=" & Range("E3") & "&q=q&y=0&z=" & _
  24.             Symbol & "&x=.csv"
  25.                   
  26. QueryQuote:
  27.             With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=DataSheet.Range("C7"))
  28.                  .AdjustColumnWidth = False
  29.                 .BackgroundQuery = True
  30.                 .TablesOnlyFromHTML = False
  31.                 .Refresh BackgroundQuery:=False
  32.                 .SaveData = True
  33.                
  34.             End With
  35.             
  36.             Range("C7").CurrentRegion.TextToColumns Destination:=Range("C7"), DataType:=xlDelimited, _
  37.                 TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
  38.                 Semicolon:=False, Comma:=True, Space:=False, other:=False
  39.             
  40.             Range(Range("C7"), Range("C7").End(xlDown)).NumberFormat = "yyyy/mm/dd"
  41.             Range(Range("D7"), Range("G7").End(xlDown)).NumberFormat = "0.00"

  42. End Sub
複製代碼

test.rar (30.19 KB)

回復 2# GBKEE


    G大還是不行
  1. Sub GetData()

  2.     Dim DataSheet As Worksheet
  3.     Dim EndDate As Date
  4.     Dim StartDate As Date
  5.     Dim Symbol As String
  6.     Dim qurl As String
  7.     Dim nQuery As Name
  8.    
  9.     Application.ScreenUpdating = False
  10.     Application.DisplayAlerts = False
  11.     Application.Calculation = xlCalculationManual
  12.    
  13.     Set DataSheet = ActiveSheet
  14.   
  15.         StartDate = DataSheet.Range("B1").Value
  16.         EndDate = DataSheet.Range("B2").Value
  17.         Symbol = DataSheet.Range("B3").Value
  18.         Range("C7").CurrentRegion.ClearContents
  19.         
  20.         qurl = qurl & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & _
  21.             "&c=" & Year(StartDate) & "&d=" & Month(EndDate) - 1 & "&e=" & _
  22.             Day(EndDate) & "&f=" & Year(EndDate) & "&g=" & Range("E3") & "&q=q&y=0&z=" & _
  23.             Symbol & "&x=.csv"
  24.             '*** 上面的網址qurl 為何要 a= b= c= d= e= 這些  ******
  25.              qurl = "http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU.php?myear=2013&mmon=9&STK_NO=2371"   
  26.                   
  27. QueryQuote:
  28.             With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=DataSheet.Range("C7"))
  29.                  .AdjustColumnWidth = False
  30.                 .BackgroundQuery = True
  31.                 .TablesOnlyFromHTML = False
  32.                 .Refresh BackgroundQuery:=False
  33.                 .SaveData = True
  34.                
  35.             End With
  36.             
  37.             Range("C7").CurrentRegion.TextToColumns Destination:=Range("C7"), DataType:=xlDelimited, _
  38.                 TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
  39.                 Semicolon:=False, Comma:=True, Space:=False, other:=False
  40.             
  41.             Range(Range("C7"), Range("C7").End(xlDown)).NumberFormat = "yyyy/mm/dd"
  42.             Range(Range("D7"), Range("G7").End(xlDown)).NumberFormat = "0.00"

  43. End Sub
複製代碼

未命名.JPG (185.66 KB)

未命名.JPG

TOP

回復 4# GBKEE


    不懂
可否詳細說明
謝謝!!

TOP

回復 4# GBKEE

G大
    那請問有辦法把http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU.php的資料
用VB的方式作成可以查詢股票代碼且有連續時間的一份資料(比方:2009/01/01到2013/09/18 )
謝謝

TOP

回復 7# GBKEE


請教 G大
根據上面執行後startdate與enddate好像有問題
跑不出來
結果如附件
謝謝!!

未命名.JPG (190.29 KB)

未命名.JPG

TOP

回復 9# GBKEE


    可以了~~感恩!!

TOP

回復 10# pupai


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

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

回復 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

        靜思自在 : 自己害自己,莫過於亂發脾氣。
返回列表 上一主題