返回列表 上一主題 發帖

[發問] 可否用迴圈或變數匯入大量資料?

本帖最後由 smart3135 於 2015-2-11 08:33 編輯
回復  smart3135
GBKEE 發表於 2015-2-10 17:29

版主您好,有試著套用您的程式碼,但在加入Refresh後會出現下圖錯誤


若是將Refresh註解跳過,是可以順利執行,不過有時候跑到一半的時候就不動了,似乎是在執行無限迴圈
必須要按ESC強制停止,再按偵錯就會跳到Do While .Busy Or .ReadyState <> 4:    Loop這段程式碼
應該就是這段程式碼在執行無限迴圈



上市年成交資訊.zip (27.04 KB)

TOP

回復 81# smart3135

在IE8下可執行,請看看你的IE [網際網路選項]需修改什麼!!
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復  smart3135

在IE8下可執行,請看看你的IE [網際網路選項]需修改什麼!!
GBKEE 發表於 2015-2-11 16:10

Hello版主,經確認之後應該是和我的系統問題有關係,因為今天重灌電腦後,再用原來的程式碼跑一次
就是最原始的那個VBA,還未加入您修改的程式碼就可以順利執行了,不過還是又跟您多學到一些了,感謝!

TOP

回復 56# GBKEE
不好意思,由於今年證交所網址大改版,原本的抓資料程式碼都會出錯,有試著看網頁原始碼修改程式碼
無奈功力太淺還是沒辦法,不知道是否還有機會請版主指點一下究竟該如何修改呢?
上市年月成交資訊.zip (62.39 KB)
  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     Set IE = CreateObject("InternetExplorer.Application")
  5.     With IE
  6.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/FMSRFK/FMSRFKMAIN.php"
  7. '        .Visible = True   '不顯示ie
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.   End With
  10. End Sub
  11. Sub 上市月成交資訊()
  12.     Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
  13.     Dim Ea As Variant, ar(), ii, aa As Integer
  14.     T = Time
  15.     Application.DisplayStatusBar = True
  16.     '請將上市的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  17.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  18.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  19.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  20.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  21.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  22.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  23.     xPath = "F:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27. MR:
  28.         With Sheets(1)
  29.             .Activate
  30.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  31.         End With
  32.         For Each X In Rng1
  33.             With IE
  34.                 .Document.getElementsByTagName("select")("query_year").Value = X
  35.                 .Document.getelementsbyname("CO_ID")(0).Value = E
  36.                 .Document.getelementsbyname("query-button")(0).Click  '按下查詢
  37.                 Do While .Busy Or .readyState <> 4:    Loop
  38.                 On Error Resume Next
  39.                 If InStr(.Document.getElementsByTagName("TABLE")(3).outerHTML, "查無") Then GoTo Nn
  40.                 If .Document.getElementsByTagName("TABLE")(3).Rows.Length > 1 Then
  41.                     Ep .Document.getElementsByTagName("TABLE")(3).outerHTML
  42.                 Else
  43.                     GoTo Nn
  44.                 End If
  45. '                If InStr(Selection.Cells(3, 1), "查無") Then Selection.Delete Shift:=xlUp: GoTo Nn
  46.             End With
  47.         With Sheets(1)
  48.             aa = Selection.Range("a3")
  49.             If aa = "" Then aa = Selection.Range("a1")    '會出錯才加入這段
  50.             If aa + 1911 <> X Then GoTo MR
  51.         End With
  52.         Next X
  53. Nn:
  54.         If Sheets(1).Range("a1") = "" Then GoTo KK
  55.         xFile = xPath & "\" & E & "\HPM.txt"
  56.         MkDir_Sub xFile
  57.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  58.         ii = ii + 1
  59.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 匯入上市月成交 " & E & "共" & ii & " 文字檔"
  60. KK:
  61.     Next E
  62.     IE.Quit
  63.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔,  讀取完畢 !! "
  64.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  65. '    ThisWorkbook.Save
  66. End Sub
  67. Sub Ep(S As String)
  68.     Dim D As New DataObject, i As Integer, FormDLL As String, Rng As Range
  69.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  70.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  71.     On Error GoTo ER
  72.     With D
  73.         .SetText S
  74.         .PutInClipboard
  75.         With Sheets(1)
  76.             With .Range("a" & .Rows.Count).End(xlUp)
  77.                 If .Row = 1 Then
  78.                     Set Rng = .Cells
  79.                 Else
  80.                     Set Rng = .Offset(1)
  81.                 End If
  82.                 Rng.Select
  83.                 .Parent.PasteSpecial Format:="Unicode 文字"
  84.                 Set Rng = Rng.Range("A3", Rng.Range("A3").End(xlDown)).Resize(, 9)
  85.                 With Sheets(1).Sort
  86.                     .SetRange Rng
  87.                     .Header = xlGuess
  88.                     .MatchCase = False
  89.                     .Orientation = xlTopToBottom
  90.                     .SortMethod = xlPinYin
  91.                     .Apply
  92.                 End With
  93.                 'Sort :資料排序
  94. '                Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  95.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  96.                 :=xlStroke, DataOption1:=xlSortNorma
  97. '                If .Row = 1 Then
  98. '                    .Range("A2").EntireRow.Delete
  99. '                Else
  100. '                    .Range("A2:A4").EntireRow.Delete
  101. '                End If
  102.             End With
  103.         End With
  104.     End With
  105.     Exit Sub
  106. ER:
  107.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\FM20.DLL"
  108.     Resume
  109. End Sub
  110. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  111.     Dim fs As Object, E As Range, C As Variant, A As String, B As Integer, D As String
  112.     Set fs = CreateObject("Scripting.FileSystemObject")
  113.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  114.     A = Q.Cells(1)
  115.     B = Len(A)
  116.         If B >= 25 Then
  117.             D = Mid(A, 11, 4)
  118.         Else
  119.             D = Mid(A, 11, 2)
  120.         End If
  121.     Q.Cells(1) = Code & "-" & D & "" & " 月成交資料"   '加入股票代號
  122.     If Q.Cells(3, 1).Offset(1) = "" Then GoTo EE
  123.     Q.Range("a3", Q.Range("a3").End(xlDown)).Replace "年度", ""
  124.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).Offset(-1).EntireRow.Delete
  125.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  126. EE:
  127.     For Each E In Q.Rows
  128.         C = Application.Transpose(Application.Transpose(E.Value))
  129.         C = Join(C, vbTab)
  130.         fs.Write C
  131.     Next
  132.     fs.Close
  133. End Sub
  134. Sub MkDir_Sub(S As String)
  135.     Dim ar, i As Integer, xPath As String
  136.     If Dir(S) = "" Then
  137.         ar = Split(S, "\")
  138.         xPath = ar(0)
  139.         For i = 1 To UBound(ar) - 1
  140.             xPath = xPath & "\" & ar(i)
  141.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  142.         Next
  143.     End If
  144. End Sub
複製代碼

上市年月成交資訊.zip (62.39 KB)

TOP

本帖最後由 GBKEE 於 2017-6-15 15:14 編輯

  1. For Each X In Rng1
  2.             With IE
  3.                 .Document.getElementsByTagName("select")("Yy").Value = X
  4.                 'yy -> 年度,mm -> 月份, dd -> 日期
  5.                 .Document.getelementsbyname("stockNo")(0).Value = E
  6.                 '股票代碼  stockNo  '**大小寫要一致**
  7.               '  .Document.getelementsbyname("query-button")(0).Click  '按下查詢
  8.                 For Each Ea In .Document.body.all.tags("a")
  9.                     If Ea.classname = "button search" Then
  10.                         Ea.Click: Exit For  '按下查詢
  11.                     End If
  12.                 Next
  13.                 Do While .Busy Or .readyState <> 4:    Loop
  14.                 On Error Resume Next
複製代碼
回復 84# smart3135
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 85# GBKEE
不好意思,我執行後似乎會卡在圖中的迴圈,不知能否請您執行看看是否有一樣情形呢?

  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     Set IE = CreateObject("InternetExplorer.Application")
  5.     With IE
  6.         .Navigate "http://www.twse.com.tw/ch/trading/exchange/FMSRFK/FMSRFKMAIN.php"
  7. '        .Visible = True   '不顯示ie
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.   End With
  10. End Sub
  11. Sub 上市月成交資訊()
  12.     Dim Rng As Range, Rng1 As Range, E As Range, X As Range, T As Date, xPath As String, xFile As String
  13.     Dim Ea As Variant, ar(), ii, aa As Integer
  14.     T = Time
  15.     Application.DisplayStatusBar = True
  16.     '請將上市的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  17.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  18.     Set Rng1 = ThisWorkbook.Sheets(3).Range("b:b")
  19.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  20.     If Application.Count(Rng1) = 0 Then MsgBox "沒有股票代號": Exit Sub
  21.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  22.     Set Rng1 = Rng1.SpecialCells(xlCellTypeConstants)
  23.     xPath = "F:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27. MR:
  28.         With Sheets(1)
  29.             .Activate
  30.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  31.         End With
  32.         For Each X In Rng1
  33.             With IE
  34.                 .Document.getElementsByTagName("select")("Yy").Value = X
  35.                 'yy -> 年度,mm -> 月份, dd -> 日期
  36.                 .Document.getelementsbyname("stockNo")(0).Value = E
  37.                 '股票代碼  stockNo  '**大小寫要一致**
  38. '                .Document.getelementsbyname("query-button")(0).Click  '按下查詢
  39.                 For Each Ea In .Document.body.all.tags("a")
  40.                     If Ea.classname = "button search" Then
  41.                         Ea.Click: Exit For  '按下查詢
  42.                     End If
  43.                 Next
  44.                 Do While .Busy Or .readyState <> 4:    Loop
  45.                 On Error Resume Next
  46.                 If InStr(.Document.getElementsByTagName("TABLE")(3).outerHTML, "查無") Then GoTo Nn
  47.                 If .Document.getElementsByTagName("TABLE")(3).Rows.Length > 1 Then
  48.                     Ep .Document.getElementsByTagName("TABLE")(3).outerHTML
  49.                 Else
  50.                     GoTo Nn
  51.                 End If
  52. '                If InStr(Selection.Cells(3, 1), "查無") Then Selection.Delete Shift:=xlUp: GoTo Nn
  53.             End With
  54.         With Sheets(1)
  55.             aa = Selection.Range("a3")
  56. '            If aa = "" Then aa = Selection.Range("a1")    '會出錯才加入這段
  57.             If aa + 1911 <> X Then GoTo MR
  58.         End With
  59.         Next X
  60. Nn:
  61.         If Sheets(1).Range("a1") = "" Then GoTo KK
  62.         xFile = xPath & "\" & E & "\HPM.txt"
  63.         MkDir_Sub xFile
  64.         Maketxt xFile, Sheets(1).Range("A1").CurrentRegion, E.Value
  65.         ii = ii + 1
  66.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 匯入上市月成交 " & E & "共" & ii & " 文字檔"
  67. KK:
  68.     Next E
  69.     IE.Quit
  70.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔,  讀取完畢 !! "
  71.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  72. '    ThisWorkbook.Save
  73. End Sub
  74. Sub Ep(S As String)
  75.     Dim D As New DataObject, i As Integer, FormDLL As String, Rng As Range
  76.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  77.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  78.     On Error GoTo ER
  79.     With D
  80.         .SetText S
  81.         .PutInClipboard
  82.         With Sheets(1)
  83.             With .Range("a" & .Rows.Count).End(xlUp)
  84.                 If .Row = 1 Then
  85.                     Set Rng = .Cells
  86.                 Else
  87.                     Set Rng = .Offset(1)
  88.                 End If
  89.                 Rng.Select
  90.                 .Parent.PasteSpecial Format:="Unicode 文字"
  91.                 Set Rng = Rng.Range("A3", Rng.Range("A3").End(xlDown)).Resize(, 9)
  92.                 With Sheets(1).Sort
  93.                     .SetRange Rng
  94.                     .Header = xlGuess
  95.                     .MatchCase = False
  96.                     .Orientation = xlTopToBottom
  97.                     .SortMethod = xlPinYin
  98.                     .Apply
  99.                 End With
  100.                 'Sort :資料排序
  101. '                Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  102.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  103.                 :=xlStroke, DataOption1:=xlSortNorma
  104. '                If .Row = 1 Then
  105. '                    .Range("A2").EntireRow.Delete
  106. '                Else
  107. '                    .Range("A2:A4").EntireRow.Delete
  108. '                End If
  109.             End With
  110.         End With
  111.     End With
  112.     Exit Sub
  113. ER:
  114.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\FM20.DLL"
  115.     Resume
  116. End Sub
  117. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  118.     Dim fs As Object, E As Range, C As Variant, A As String, B As Integer, D As String
  119.     Set fs = CreateObject("Scripting.FileSystemObject")
  120.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  121.     A = Q.Cells(1)
  122.     B = Len(A)
  123.         If B >= 25 Then
  124.             D = Mid(A, 11, 4)
  125.         Else
  126.             D = Mid(A, 11, 2)
  127.         End If
  128.     Q.Cells(1) = Code & "-" & D & "" & " 月成交資料"   '加入股票代號
  129.     If Q.Cells(3, 1).Offset(1) = "" Then GoTo EE
  130.     Q.Range("a3", Q.Range("a3").End(xlDown)).Replace "年度", ""
  131.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).Offset(-1).EntireRow.Delete
  132.     Q.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  133. EE:
  134.     For Each E In Q.Rows
  135.         C = Application.Transpose(Application.Transpose(E.Value))
  136.         C = Join(C, vbTab)
  137.         fs.Write C
  138.     Next
  139.     fs.Close
  140. End Sub
  141. Sub MkDir_Sub(S As String)
  142.     Dim ar, i As Integer, xPath As String
  143.     If Dir(S) = "" Then
  144.         ar = Split(S, "\")
  145.         xPath = ar(0)
  146.         For i = 1 To UBound(ar) - 1
  147.             xPath = xPath & "\" & ar(i)
  148.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  149.         Next
  150.     End If
  151. End Sub
複製代碼
上市月成交資訊.zip (39.22 KB)

TOP

回復 86# smart3135

證交所網頁有流量管制.附檔有重新修改一些地方.

上市月成交資訊.rar (52.87 KB)
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 87# GBKEE
感謝版主阿,真的太實用了,我還有自己修改一些內容符合我想要的格式
另外上市年成交資訊是依您前一篇回覆的內容我也稍作修改,一樣可以正常抓資料了
真是受教了:handshake

TOP

[版主管理留言]
  • GBKEE(2018/2/12 17:12): 證交所改版後,用程式大量下載資料有 IP 的管制, 請另找相同性質網頁看看

回復 87# GBKEE
版主您好,又來請教您了,半年前證交所改版後有請您幫忙,後來確實可以抓資料了
但今年測試,下載沒幾個就會卡住,我再去開證交所網頁就會開不起來
有請朋友幫忙測試,他們的電腦是可以開的,這是不是表示因為我在抓大量資料
所以會有流量或頻寬的限制?連結中的文章似乎也是說明如此
不知道程式碼還有沒有能修正的地方讓它可以正常抓資料,謝謝
https://www.wearn.com/bbs/t911387.html
上市年成交資訊.zip (26.12 KB)
上市月成交資訊.zip (40.75 KB)

TOP

回復 89# smart3135
感謝版主回覆,找了一下,找不到相關的網頁,目前可以使用的方式就是用一樣的程式每次資料只能抓25筆左右
當無法抓的時候就將數據機關掉重開,等網路連線就可以再抓25筆,900多筆資料要慢慢抓,哈

TOP

        靜思自在 : 我們要做好社會的環保,也要做好內心的環保。
返回列表 上一主題