返回列表 上一主題 發帖

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

本帖最後由 smart3135 於 2014-5-22 09:35 編輯

回復 64# GBKEE
版主您好,今天利用上櫃月成交資料的程式碼來做了一些修正,已經可以順利擷取上櫃年成交資料了,只是還有一些小問題:
1.多餘的語法該刪除的我應該都刪除了,不確定有沒有多餘不必要的語法沒被刪除
2.在擷取資料貼到EXCEL後,日期的部分會變成文字,請見附圖,不過網頁顯示的只是單純的日期
舉例來說:網頁顯示的表格是4/17,但匯入EXCEL後就會變成4月17日,而寫入文字檔時則變成2014/4/17
我有試著在匯入EXCEL之前將日期欄位的儲存格格式先設定成文字,不過貼上EXCEL後還是會被修改格式
我希望寫入文字檔的日期資料只要月日就好,也就是4/17,不知道這部分有沒有辦法修改?
另外如65#回覆,集保戶資料的問題還是未能解決,再麻煩您幫忙看一下囉!感謝!

  1. Option Explicit
  2. Dim IE As Object
  3. Sub IE_Application()
  4.     If Not IE Is Nothing Then IE.Quit  '當查到上櫃的年份就會出現查無,此IE無法再度查詢,關閉它
  5.     Set IE = CreateObject("InternetExplorer.Application")
  6.     With IE
  7.         .Navigate "http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php"
  8. '        .Visible = True   '不顯示ie
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.     End With
  11. End Sub
  12. Sub 上櫃年成交資訊()
  13.     Dim Rng As Range, Rng1 As Range, E As Range, T As Date, xPath As String, xFile As String
  14.     Dim Ea As Variant, AR(), ii As Integer
  15.     T = Time
  16.     Application.DisplayStatusBar = True
  17.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  18.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  19.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  20.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  21.     xPath = "G:\財報資料"
  22.     IE_Application
  23.     Application.StatusBar = " "
  24.     For Each E In Rng
  25.         Sheets(1).UsedRange.Clear            '下載資料置於此工作表,變換股票時:清空
  26.             With IE
  27.                  With .document.getelementbyid("input_stock_code")
  28.                     .Value = E
  29.                     .ParentNode.submit
  30.                 End With
  31.                 Do While .Busy Or .readyState <> 4:    Loop
  32.                 If Application.Count(Sheets(1).UsedRange) = 0 Then '此工作表清空時:下載第一年度時
  33.                     AR = Array(0, 2)
  34. '                Else
  35. '                    AR = Array(2)
  36.                 End If
  37.                 For Each Ea In AR
  38.                     Ep .document.getElementsByTagName("TABLE")(Ea).outerHTML
  39.                 Next
  40.             End With
  41.         ii = ii + 1
  42.         xFile = xPath & "\" & E & "\HPM.txt"
  43.         MkDir_Sub xFile
  44.         Maketxt xFile, Sheets(1).UsedRange
  45.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃" & E & "年成交 " & ii & " 文字檔"
  46.     Next E
  47.     IE.Quit
  48.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上櫃年成交 " & ii & " 文字檔,  讀取完畢 !! "
  49.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  50. End Sub
  51. Sub Ep(S As String)
  52.     Dim D As New DataObject, E As Shape, FormDLL As String, Rng As Range
  53.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  54.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  55.     On Error GoTo ER
  56.     With D
  57.         .SetText S
  58.         .PutInClipboard
  59.         With Sheets(1)
  60.             .Range("a" & .Rows.Count).End(xlUp).Select
  61.             If .Range("a1") <> "" Then .Range("a" & .Rows.Count).End(xlUp).Offset(1).Select
  62.             .PasteSpecial Format:="Unicode 文字"
  63.         End With
  64.     End With
  65.     Exit Sub
  66. ER:
  67.     FormDLL = "FM20.DLL"
  68.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  69.     Resume
  70. End Sub
  71. Sub MkDir_Sub(S As String)
  72.     Dim AR, i As Integer, xPath As String
  73.     If Dir(S) = "" Then
  74.         AR = Split(S, "\")
  75.         xPath = AR(0)
  76.         For i = 1 To UBound(AR) - 1
  77.             xPath = xPath & "\" & AR(i)
  78.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  79.         Next
  80.     End If
  81. End Sub
  82. Sub Maketxt(xF As String, Q As Range)    '將匯入資料存入指定的txt
  83.     Dim fs As Object, E As Range, C As Variant
  84.     Q.Range("C1").Clear
  85.     Q.Range("A1") = Q.Range("B1") & " " & "年成交資料"
  86.     Q.Range("B1").Clear
  87.     Q.Rows(2).Delete
  88.     Set fs = CreateObject("Scripting.FileSystemObject")
  89.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  90.     For Each E In Q.Rows
  91.         C = Application.Transpose(Application.Transpose(E.Value))
  92.         C = Join(C, vbTab)
  93.         fs.WriteLine C
  94.     Next
  95.     fs.Close
  96. End Sub
複製代碼
上櫃年成交資訊.zip (20.05 KB)

TOP

回復 67# GBKEE
版主您好,因為我有雙系統,一個是WIN7+2007,一個是XP+2003,我平常都是開WIN7的,經你提醒,今天試著用2003跑一次集保程序
結果真的可以執行,不會出錯,但在2007卻會出錯,這部分可能還要再研究一下為什麼會這樣
另外寫入日期部分經帶入您的程式碼後已可正常寫入日期,再次感謝您大力協助,謝謝您!

TOP

本帖最後由 smart3135 於 2014-5-23 11:13 編輯

回復 67# GBKEE
版主您好,經過今天早上不斷使用2003版測試,終於做出我想要的輸出文字檔結果,不過還是有些問題會發生,先附上程式碼與附檔
集保戶-新.zip (128.04 KB)
這裡是完整的程式碼
  1. Option Explicit
  2. Dim IE As Object, A As Integer
  3. Sub IE_Application()
  4.     Dim I As Integer
  5.     Set IE = CreateObject("InternetExplorer.Application")
  6.     With IE
  7.         .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
  8. '        .Visible = True   '不顯示ie
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.         '讀取集保戶股權分散表查詢的資料日期總個數
  11.         A = .document.getelementsByTagName("select")("SCA_DATE").Length - 1
  12.   End With
  13. End Sub
  14. Sub 集保()
  15.     Dim Rng As Range, E As Range, x As Variant, T As Date, xPath As String, xFile As String
  16.     Dim Ea As Variant, ii As Integer, F As String, H As String, J As Integer
  17.     T = Time
  18.     Application.DisplayStatusBar = True
  19.     '請將上櫃的股票代號,在 Sheets(3).Range("A1")往下Key上,迴圈依這裡的股票代號匯入
  20.     Set Rng = ThisWorkbook.Sheets(3).Range("A:A")
  21.     If Application.Count(Rng) = 0 Then MsgBox "沒有股票代號": Exit Sub
  22.     Set Rng = Rng.SpecialCells(xlCellTypeConstants)
  23.     xPath = "E:\財報資料"
  24.     IE_Application    '
  25.     Application.StatusBar = " "
  26.     For Each E In Rng
  27.         With Sheets(1)
  28.             .Activate
  29.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  30.         End With
  31.             With IE
  32.                 .document.getElementById("StockNo").Value = E
  33.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  34.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  35.                 Do While .Busy Or .readyState <> 4:    Loop
  36.                 Ep .document.getelementsByTagName("TABLE")(5).outerHTML
  37.             End With
  38.         
  39.         For x = 0 To A
  40.             With IE
  41.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  42.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  43.                 .document.getElementById("StockNo").Value = E
  44.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  45.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  46.                 Do While .Busy Or .readyState <> 4:    Loop
  47.                 Ep .document.getelementsByTagName("TABLE")(6).outerHTML
  48.             End With
  49.             With IE
  50.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  51.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  52.                 .document.getElementById("StockNo").Value = E
  53.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  54.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  55.                 Do While .Busy Or .readyState <> 4:    Loop
  56.                 Ep .document.getelementsByTagName("TABLE")(7).outerHTML
  57.             End With
  58.         Next x
  59.         With Sheets(1)
  60.             F = .Range("a3")
  61.             J = Len(F)
  62.             If J >= 19 Then
  63.                 H = Mid(F, 1, 3)
  64.             Else
  65.                 H = Mid(F, 1, 2)
  66.             End If
  67.             .Range("a1") = E & "-" & H & " " & "集保戶股權分散表"
  68.             .Rows("2:4").Delete
  69.         End With
  70.         xFile = xPath & "\" & E & "\SHD.txt"
  71.         MkDir_Sub xFile
  72.         Maketxt xFile, Sheets(1).UsedRange, E.Value
  73.         '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  74.         'xFile(第一個引數), Sheets(1).Range("A1").CurrentRegion(第二個引數),E.Value(第三個引數)
  75.         ii = ii + 1
  76.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔"
  77.     Next E
  78.     IE.Quit
  79.     Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔,  讀取完畢 !! "
  80.     MsgBox "匯入 文字檔" & ii & " 費時 " & Application.Text(Time - T, ["MM分SS秒"])
  81. '    ThisWorkbook.Save
  82. End Sub
  83. Sub Ep(S As String)
  84.     Dim D As New DataObject, E As Shape, FormDLL As String, Rng As Range
  85.     'DataObject 物件 在進行轉換動作時,做為格式化文字資料的暫存區域。其也可以暫存和儲存在 DataObject 的文字片段相關的格式。
  86.     '宣告 Dim D As New DataObject '須在工具-> 設定引用項目加入 新增引用 Microsoft Forms 2.0 Object Library ,專案 加入一表單即可
  87.     On Error GoTo ER
  88.     With D
  89.         .SetText S
  90.         .PutInClipboard
  91.         With Sheets(1)
  92.             .Range("a" & .Rows.Count).End(xlUp).Select
  93.             Set Rng = Selection
  94.             If Rng = 15 Then
  95.                 Rng.Offset(3).Select
  96.             Else
  97.                 Rng.Offset(2).Select
  98.             End If
  99.             .PasteSpecial Format:="Unicode 文字"
  100. '            If Selection = "" Then Selection.Offset(1).Select
  101. '            Set Rng = Selection
  102. '                Rng.Sort Key1:=Rng.Range("B2"), Order1:=xlDescending, Header:=xlYes ', _
  103.                 OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  104.                 :=xlStroke, DataOption1:=xlSortNorma
  105.         End With
  106.     End With
  107.     Exit Sub
  108. ER:
  109.     FormDLL = "FM20.DLL"
  110.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  111.     Resume
  112. End Sub
  113. Sub Maketxt(xF As String, Q As Range, Code As String)     '將匯入資料存入指定的txt
  114.           '***想請問您Code As String的Code是怎麼帶出個股編號的 ****
  115.           ' xF(接收的引數名稱) As String(字串型態), Q As Range(Range物件型態), Code As String(字串型態)
  116.     Dim fs As Object, E As Range, C As Variant
  117.     Set fs = CreateObject("Scripting.FileSystemObject")
  118.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  119.     For Each E In Q.Rows
  120.         C = Application.Transpose(Application.Transpose(E.Value))
  121.         C = Join(C, vbTab)
  122.         fs.WriteLine C
  123.     Next
  124.     fs.Close
  125. End Sub
  126. Sub MkDir_Sub(S As String)
  127.     Dim AR, I As Integer, xPath As String
  128.     If Dir(S) = "" Then
  129.         AR = Split(S, "\")
  130.         xPath = AR(0)
  131.         For I = 1 To UBound(AR) - 1
  132.             xPath = xPath & "\" & AR(I)
  133.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  134.         Next
  135.     End If
  136. End Sub
複製代碼
因為我要的文字檔除了個股代號和名稱之外,還有日期也要一併寫入,所以我就自己加了以下這些東西
  1.             With IE
  2.                 .document.getElementById("StockNo").Value = E
  3.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  4.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  5.                 Do While .Busy Or .readyState <> 4:    Loop
  6.                 Ep .document.getelementsByTagName("TABLE")(5).outerHTML
  7.             End With
  8.         
  9.         For x = 0 To A
  10.             With IE
  11.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  12.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  13.                 .document.getElementById("StockNo").Value = E
  14.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  15.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  16.                 Do While .Busy Or .readyState <> 4:    Loop
  17.                 Ep .document.getelementsByTagName("TABLE")(6).outerHTML
  18.             End With
  19.             With IE
  20.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  21.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  22.                 .document.getElementById("StockNo").Value = E
  23.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  24.                        '這裡的查詢是<INPUT TYPE="submit" VALUE="查詢" name="sub">
  25.                 Do While .Busy Or .readyState <> 4:    Loop
  26.                 Ep .document.getelementsByTagName("TABLE")(7).outerHTML
  27.             End With
複製代碼
1.首先第一段是為了要擷取("TABLE")(5)才能匯入個股名稱,所以我加了這些語法,由於個股名稱只需要擷取一次
    所以將這段語法寫在x迴圈之外,但不知道這樣的語法正不正確
2.因為要擷取的日期是在("TABLE")(6),所以將這段也寫入,就只是複製("TABLE")(7)的改成6而已,也不確定這樣寫到底正不正確
3.以上程式碼在2003使用F8逐行執行時是可以正常的,但使用F5連續執行時有時會出錯,不然就是("TABLE")(5)和("TABLE")(6)會有資料重覆或者漏抓
    以("TABLE")(6)抓到的日期來說,會抓到兩個一樣的日期(日期錯誤),但資料內容的("TABLE")(7)卻是不同的(內容正確),也就是日期和資料內容對不上
    不知道是不和我的語法有問題

另外先前向您提過,用2007執行上述程式碼時會出錯,出錯代碼為"424",出錯訊息為"此處需要物件",我有點說明進去看,但真的看不太懂
我將說明內容存成PDF一起放在附件中,可以的話再麻煩您看看是不是可以找出為什麼這個程式碼沒法在2007執行的原因,再次感謝您!

TOP

回復 70# GBKEE
哈囉!版主您好,再度將您修正過後都程式碼代入之後仍是在相同位置出現"424"的錯誤,由於您的EXCEL版本並非2007,所以也沒辦法一一測試我的錯誤
看來這個問題似乎需要再慢慢研究
我用2003執行是正常的,不過在執行的過程中,我發現抓下來的資料會有一些問題

如圖,有些日期的資料是重覆的,有些日期的資料卻漏抓,103年3月份抓了2次,103年2月卻漏抓,但這個情況只有在按下F5連續執行時才會發生
按F8逐行執行則不會,不知道為什麼會這樣?想請問兩個問題
1.這種狀況有辦法加入其他程式碼來避免嗎?
2.就我的感覺應該是F5連續執行的速度太快,以致於網頁反應不及,還來不及跳到下一筆日期就抓資料了才會出錯,不知道這樣推斷正不正確?

附上完整的文字檔,再麻煩您幫忙看看囉!感謝!
1103嘉泥集保戶.zip (1.99 KB)

TOP

回復 70# GBKEE
版主您好,我在無意中發現了可以解決錯誤的方法,也不需要再另外設物件,只要將.document.getElementById("StockNo").Value=E
改成.document.All("StockNo").Value=E就可以了,也就是getElementById改成All就可以正常執行了
只是不清楚為什麼這樣改就可以了
  1.     For Each E In Rng
  2.         With Sheets(1)
  3.             .Activate
  4.             .Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  5.         End With
  6.         For x = 0 To A
  7.             With IE
  8.                 .document.getelementsByTagName("select")("SCA_DATE")(x).Selected = True
  9.                                             '這裡的資料日期 需用 Select 它是有[年度月份日期]的
  10.                 .document.All("StockNo").Value = E
  11.                 .document.getelementsByTagName("INPUT")("sub").Click  '按下查詢
  12.                 Do While .Busy Or .readyState <> 4:    Loop
  13.                 If x = 0 Then Sheets(1).Cells(1) = .document.getelementsByTagName("TABLE")(5).INNERTEXT
  14.                 Ep .document.getelementsByTagName("TABLE")(6).INNERTEXT
  15.                 Ep .document.getelementsByTagName("TABLE")(7).outerHTML
  16.             End With
  17.         Next x
  18.         xFile = xPath & "\" & E & "\SHD.txt"
  19.         MkDir_Sub xFile
  20.         Maketxt xFile, Sheets(1).UsedRange, E.Value
  21.         ii = ii + 1
  22.         Application.StatusBar = Application.Text(Time - T, ["MM分SS秒"]) & " 共匯入上市月成交 " & ii & " 文字檔"
  23.     Next E
複製代碼
不過又遇到了另一個問題,就是當個股遇到沒當年度月份的資料時,就會出現查無資料的視窗,這是連續執行的結果


而當我用F8逐行執行的結果,當遇到查無年度月份資料時,就會一直在Do While .Busy Or .readyState <> 4:    Loop執行無窮迴圈
請問這部分是否有辦法解決?我個人猜想應該是要修正變數A或x的程式碼,但卻不清楚該怎麼修改,只好再麻煩您幫忙看看了,謝謝!

TOP

回復 73# GBKEE
版主您好,試著將您新增的程式碼代入,的確是可以避免錯誤的發生,但在執行程式後會變成擷取的資料不完整
例如1301的資料是從2014年5月到2013年5月,可是在擷取資料時卻沒辦法抓下完整資料,有時只抓到一個月,有時只抓到三個月
不知您是否能幫忙執行看看是否和我說的結果相同
相較於之前還未加入您在73#的程式碼,雖然當遇到查無日期會出錯,不過當有完整一年份資料日期時
抓到的資料都是完整的
我自己也有適著寫入一些程式碼,或著變換程式碼的一些先後位置,但執行結果都不理想
不知道有沒有更適合的程式碼,能避免出錯,也能讓抓到的資料是完整的,再麻煩您幫忙看看了,謝謝!
附帶一提,不會出錯的那個程式用F8逐行執行似乎可以抓到完整資料,但用F5連續執行,抓到的資料就會不完整
不知道是不是因為用連續執行的速度太快,網頁來不及反應,所以抓到的資料才不完整
集保兩個程式+兩個資料不完整文字檔.zip (47.24 KB)

TOP

回復 73# GBKEE
版主,不好意思,上一篇回覆其中一個檔案有錯誤,我再重新附上
另外我花了一些時間做出了另一個版本的程式,是利用儲存格輸入日期當作迴圈,已測試可以抓資料,但有些小問題:
1.有些語法我不太懂怎麼簡化,所以可能寫的比較複雜一點
2.利用儲存格當日期迴圈的缺點,就是每個月都要更新儲存格中的日期
3.我自己寫的程式碼執行到代碼1340時還是會出錯,不過如果把第一個代碼重新設成1340開始抓資料又正常
4.因為表頭的文字會重覆抓取,但我只需要一次,所以用一列一列刪除的笨方法

這個程式寫的比較粗糙,抓資料的速度似乎也比較慢,不過確實可以達到我需要的結果,除了遇到某些代碼會卡住需要重新設定外
其他都還OK,再請您幫忙看一下是否有錯誤的地方需要修正,謝謝!
集保新的.zip (42.48 KB)
  1. Option Explicit
  2. Sub 集保完成()
  3.     Dim E As Range, X As Range, URL As String, xPath As String, xFile As String, rng As Range, rng1 As Range
  4.     Dim Msg As Boolean, I As Integer, t As Date, S As String, BB As String, CC As String, rng2 As Range
  5.     t = Time
  6.     URL = "URL;http://www.tdcc.com.tw/smWeb/QryStock.jsp?SCA_DATE="
  7.     BB = "&SqlMethod=StockNo&StockNo="
  8.     CC = "&sub=%ACd%B8%DF"
  9.     xPath = "D:\財報資料"
  10.     With ThisWorkbook
  11.         With .Sheets(3)
  12.             Set rng = .Range("A1", .Range("A1").End(xlDown))
  13.             Set rng1 = .Range("B1", .Range("B1").End(xlDown))
  14.         End With
  15. '        .Sheets(3).Activate    '兩種寫法都可以 不過第一種比較簡化 所以第二種跳過
  16. '        .Sheets(3).Range("a1").Select
  17. '        Range(Selection, Selection.End(xlDown)).Select
  18. '        Set rng = Selection
  19. ''        Set rng = .Sheets(3).Range("A:A")  '這裡這樣設定會變成無窮迴圈
  20. '        .Sheets(1).Activate
  21.         With .Sheets(1)      '活頁簿的第 1 張工作表
  22.             If .QueryTables.Count = 0 Then
  23.                 With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
  24.                     .Refresh BackgroundQuery:=False
  25.                 End With
  26.             End If
  27.             
  28.             For Each E In rng
  29.                 With ThisWorkbook
  30.                 .Sheets(2).Cells.Clear
  31. '                .Activate
  32.                 .Sheets(1).Cells.Clear  '下載資料置於此工作表,變換股票時:清空
  33.             End With

  34.                 For Each X In rng1
  35.                     With .QueryTables(1)
  36.                         .Connection = URL & X & BB & E & CC
  37.                         .PreserveFormatting = True
  38.                         .BackgroundQuery = True
  39.                         .RefreshStyle = xlInsertDeleteCells
  40.                         .SaveData = True
  41.                         .AdjustColumnWidth = True
  42.                         .RefreshPeriod = 0
  43.                         .WebSelectionType = xlSpecifiedTables
  44.                         .WebFormatting = xlWebFormattingNone
  45.                         .WebTables = "6,7,8"
  46.                         On Error GoTo xlnext
  47.                         .WebPreFormattedTextToColumns = True
  48.                         .WebConsecutiveDelimitersAsOne = True
  49.                         .Refresh BackgroundQuery:=False
  50.                     End With
  51.                     Set rng2 = Sheets(1).UsedRange
  52.                     If Sheets(2).Range("a1") = "" Then
  53.                         rng2.Copy Sheets(2).Range("a" & .Rows.Count).End(xlUp)
  54.                     Else
  55.                         rng2.Copy Sheets(2).Range("a" & .Rows.Count).End(xlUp).Offset(2, 0)
  56.                     End If
  57.                 Next X
  58. xlnext:
  59.                     Sheets(2).Range("2:2,22:22,43:43,64:64,85:85,106:106,127:127,148:148,169:169,190:190,211:211,232:232,253:253").Delete
  60.                     xFile = xPath & "\" & E & "\SHD.txt"
  61.                     MkDir_Sub xFile       '10#的程式 'C槽下的季損益表資料夾不需先建立
  62.                     Maketxt xFile, Sheets(2).UsedRange

  63. '                S = " " & Sheets(1).QueryTables(1).ResultRange(1)
  64. '                If Val(S) < 0 Then S = " 查無"
  65.                 I = I + 1
  66.                 Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & "  " & E & "匯入" & I & "個文字檔"
  67.                 Msg = False
  68.             Next E
  69.         End With
  70.     End With
  71.     MsgBox "共匯入 文字檔" & I & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
  72. End Sub
  73. Sub MkDir_Sub(S As String)
  74.     Dim AR, I As Integer, xPath As String
  75.     If Dir(S) = "" Then
  76.         AR = Split(S, "\")
  77.         xPath = AR(0)
  78.         For I = 1 To UBound(AR) - 1
  79.             xPath = xPath & "\" & AR(I)
  80.             If Dir(xPath, vbDirectory) = "" Then MkDir xPath
  81.         Next
  82.     End If
  83. End Sub
  84. Sub Maketxt(xF As String, Q As Range)   '將匯入資料存入指定的txt
  85.     Dim fs As Object, E As Range, C As Variant
  86.     Set fs = CreateObject("Scripting.FileSystemObject")
  87.     Set fs = fs.CreateTextFile(xF, True)  '創見一個檔案,如檔案存在可覆蓋掉
  88.     For Each E In Q.Rows
  89.         C = Application.Transpose(Application.Transpose(E.Value))
  90.         C = Join(C, vbTab)
  91.         fs.WriteLine C
  92.     Next
  93.     fs.Close
  94. End Sub
複製代碼

TOP

回復 73# GBKEE
Sorry,儲存格又忘了填上資料,再附上正確檔案
集保-設定日期為儲存格迴圈.zip (26.86 KB)

TOP

回復  smart3135
GBKEE 發表於 2014-5-26 17:13

GBKEE版主您好,去年有向您請教如何用迴圈抓取股市大量資料,幫助非常大,今年已經有新的年度資料,我想要抓取新的年度資料
但不知道為什麼去年度可以正常執行的VBA程式碼,到了今年卻會一直出錯,我嘗試新增、刪除、變更一些程式碼,但都無效
能不能請您再次幫忙看看程式碼有什麼問題嗎?感謝您!
上市年成交資訊.zip (22.07 KB)
出錯圖

TOP

本帖最後由 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

        靜思自在 : 靜坐常恩己過、閒談莫論人非。
返回列表 上一主題